问题
Just want to save Date as timestamp in Firestore database but it's storing timestamp as number:
save() {
const data = this.transactionForm.value;
data.type = data.type == 'true' || data.type == true ? true : false;
data.updated = firebase.firestore.FieldValue.serverTimestamp();
// Date string to timestamp
data.date = new Date(data.date.split("-").reverse().join("-")).getTime();
let id = data.id;
this.transCollection.doc(id).update(data);
}
回答1:
unixToTimestamp() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
console.log(hours + ':' + minutes + ':' + seconds);
console.log(day + '/' + month + '/' + year);
}
I tried this out in typescript, and i got the following output:
11:25:07
19/11/2018
This way you can choose how you want to create your timestamp and save it in firestore
You could also print out month the way you want.
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const month = months[date.getMonth()];
This is because date.getMonth() gets index, which is why i use +1 to get a month.
回答2:
I use timestamp in Firebase and I know it is use to store date in long
which is number. So I dont know if it is the same in Firestore.
来源:https://stackoverflow.com/questions/53372270/how-to-save-data-as-firestore-timestamp-object-in-angular