[英]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]
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.