Reputation: 6947
I'm trying to store a timestamp in Firestore from Firebase Functions. However, the incorrect time is being recorded in Firestore.
For instance, I want to store the UTC time: August 14, 2021 21hr (9pm)
Note: this code is in my Firebase Function
const date = new Date(Date.UTC(
csvRow.startDateYear,
csvRow.startDateMonth,
csvRow.startDateDay,
csvRow.startDateHour,
csvRow.startDateMinute
))
console.log(date); // prints 2021-08-14T21:00:00.000Z (correct)
console.log('date.getTime():' + date.getTime()); // prints 1628974800000 (correct)
const timeStamp = admin.firestore.Timestamp.fromMillis(date.getTime());
console.log('timeStamp.toMillis():' + timeStamp.toMillis()); // prints 1628974800000 (correct)
write.startDate = timeStamp;
But when I actually look at the Firestore and see what's recorded, I get this, a timeStamp that's 7 hours behind:
I've also tried Timestamp.fromDate(date)
but got the same result
Upvotes: 3
Views: 479
Reputation: 1256
Firebase Firestore TimeStamps break apart when using hours because they do not have timezones built into them. They are meant to represent a greater period of time, not for specific timezones (hour).
Try this function and see if it works properly:
var myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
If you are attempting this with Cloud Functions try the following:
admin.firestore.Timestamp.fromDate()
Detailed documentation: https://firebase.google.com/docs/reference/node/firebase.firestore.Timestamp
Upvotes: 1