简体   繁体   English

如何将 firebase 中的时间戳数据类型转换为日期?

[英]How to convert timestamp data type in firebase to date?

I have this in my firebase database.我的 firebase 数据库中有这个。 beginDate: December 25, 2018 at 4:00:00 PM UTC-8.开始日期:2018 年 12 月 25 日下午 4:00:00 UTC-8。 I want this to convert to Date with time.我希望它随时间转换为日期。

I tried this and it is not working我试过了,但没用

{{ (elem.beginDate*1000) | {{ (elem.beginDate*1000) | date }}.日期 }}。

I am building an app on Ionic and my template has this code.我正在 Ionic 上构建一个应用程序,我的模板有这段代码。

From: {{ (elem.beginDate*1000 | date) }}来自:{{(elem.beginDate*1000 | 日期)}}

which gives an output [object Object]给出输出 [object Object]

Firestore timestamps aren't returned as numbers. Firestore 时间戳不会以数字形式返回。 They're Timestamp objects.它们是时间戳对象。 If you want to convert a Timestamp to a JavaScript Date, use its toDate() method.如果要将时间戳转换为 JavaScript 日期,请使用其toDate()方法。

First you retrieve the timestamp from firebase then you convert the timestamp to a date with toDate().首先从 firebase 检索时间戳,然后使用 toDate() 将时间戳转换为日期。 You can also use toMillis().您还可以使用 toMillis()。 More information on: https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp.html#returnsnumber有关更多信息: https ://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp.html#returnsnumber

db.collection('user').get().then( (snapshot) => {
    snapshot.docs.forEach(doc => {
        var time = doc.data().timestamp;
        var date = time.toDate();
        console.log(date);
    })
})

What I was getting from Firestore was this: t {seconds: 1622775600, nanoseconds: 0}我从 Firestore 得到的是:t {seconds: 1622775600, nanoseconds: 0}

And this is what worked for me to be able to convert it to the date format:这就是我能够将其转换为日期格式的原因:

  return this.collection.snapshotChanges().
     pipe(map(actions =>{
        return actions.map(item =>{
            const data = item.payload.doc.data() as [myObject];

            data.beginDate = new Date(data.beginDate["seconds"] * 1000);

            const id = item.payload.doc.id;
            return {...data,id};
        })
    }));

I hope this is useful to you.我希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将日期转换为时间戳以存储到 javascript 中的 firebase firestore - Convert date to timestamp for storing into firebase firestore in javascript 如何将时间戳从 firebase 转换为 DateTime 和 flutter - How to convert Timestamp to DateTime from firebase with flutter 如何将 firebase 时间戳转换为字符串 Android - How to convert firebase Timestamp to String Android 如何在typescript函数中获取Firebase中的TImestamp类型? - How to get TImestamp type in Firebase functions in typescript? 如何在 Google Data Studio 中将文本类型转换为日期类型? - How to Convert Text type to Date type in Google Data Studio? Firebase TIMESTAMP 到日期和时间 - Firebase TIMESTAMP to date and Time Vue Firebase - 将 Firestore 时间戳值数组转换为可读的 javascript 日期 - Vue Firebase - convert array of firestore timestamp value to readable javascript date 将 firebase 时间戳转换为雪花时间戳 - Convert firebase timestamp to snowflake timestamp 如何将 Firestore 日期/时间戳转换为 JS Date()? - How do I convert a Firestore date/Timestamp to a JS Date()? 如何将 Firestore 日期/时间戳转换为 Kotlin 中的日期? - How do I convert a Firestore date/Timestamp to Date in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM