How to save date in Angular as a timestamp to Firestore?

夙愿已清 提交于 2019-12-02 04:37:07

If you want in your Cloud Firestore database the date value of:

firebase.firestore.FieldValue.serverTimestamp()

You should use a call to .toDate() function. Please also see FireStore Timesteamp official documentation for more informations. In your particular case, you should use the following call:

treatment.activationDate.toDate()

This change was added in Firebase JS SDK v4.13.0, where JavaScript Date objects should be saved of type Timestamp.

I save my data to firestore and use new Date(). The date is currently added as a timestamp to firestore.

But you can use moment.js in your app to handle the string. moment('my date in a string format') will be converted as you like.

Since my date was converted into string at some point, I converted it back to Date object just before it is being saved to the database.

let myDate = new Date();
// myDate is converted to string at some point.

Just before I save it to firebase, I needed to convert it again to Date object.

// need to parse from String to Date
let myDateTemp = new Date(myDate);
record = {
    myDate: myDateTemp
}
return this.firestore.collection('myCollection').add(record);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!