Skip to content

Instantly share code, notes, and snippets.

@discoNeko
Last active January 2, 2020 17:15
Show Gist options
  • Save discoNeko/51177f94a2b26fa320c9bf23fbde1e74 to your computer and use it in GitHub Desktop.
Save discoNeko/51177f94a2b26fa320c9bf23fbde1e74 to your computer and use it in GitHub Desktop.

firebaseとdate型 firebaseに保存されている日付データの型はいくらか種類があるので調べる

firebase/authentication

認証時に保存されるユーザ作成日

const admin = require('firebase-admin')
admin.auth().listUsers.filter(user => {
    console.log(user.metadata.creationTime) // javascriptのdate型
})

firebase/database

databaseに保存される日付のフィールド フロントエンド側でnew Date()を与えてもdatabaseではTimestamp型として保存される

const collections = await admin.firestore().collection('collections').get()
collections.docs.filter(collection => {
    console.log(collection.data().createdAt) // Timestamp型
    console.log(collection.data().createdAt.toDate()) // javascriptのdate型
})

firebase実行環境の時間取得

GMT+0000(UTC)の時刻が取得される

const today = new Date()
console.log(today) // ex.2020-01-01T15:36:00.639Z

firebase/databaseの時間取得

firebase console(GUI)上ではGMT+0900(日本標準時)に合わせた時刻が表示されているが、 firebaseから取得するとGMT+0000(UTC)の時刻となっている

const collections = await admin.firestore().collection('collections').get()
collections.docs.filter(collection => {
    console.log(collection.data().createdAt.toDate()) // ex.2020-01-02T15:36:00.639Z
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment