1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

分析屋が異世界転生してエンジニアになったAdvent Calendar 2022

Day 9

TypeScriptでFireBaseのTimeStamp型を扱う

Last updated at Posted at 2022-12-08

はじめに

FireBaseのTimeStamp型のデータ同士を計算したい時に変換で少し詰まったので簡単にまとめてみます。

やりかた

これでいけました

    const newArray = [...datas, newData].sort(
      (a, b) => b.createdAt.toDate().getTime() - a.createdAt.toDate().getTime()
    );

細かく分解すると

createdAt(Firebaseで用意されているTimeStamp型)

nt {seconds: 1668498018, nanoseconds: 349000000}

toDate()でDate型に変換

Tue Nov 15 2022 16:40:18 GMT+0900 (日本標準時)

getTime()で日付と時刻の値を1970年1月1日 00:00:00 UTCからの経過ミリ秒の値に変換

1668498018349

おわりに

時刻系の変換は毎回詰まるので慣れていきたいです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?