timestamp in firebase/firestore - javascript

When I save timestamp dates on firestore, it saves as a timestamp datatype.
But when I retrieve the data into React, sometimes it is retrieved as a timestamp and I can convert it to JS date using toDate() but other times is retrieved as {_seconds, _nanoseconds} and I need to convert it manually.
It seems very confusing. Is there a best practise about how to save and how to retrieve dates from Google Firestore?
Thanks

The only way to store dates in Firestore is through the Timestamp, you could store them through Strings, but it would be a huge pain to convert it back to Date so there really isn't any other better way to do it, unfortunately.

Related

How do I convert a date retrieved by API to a date understood by React

In my app, I retrieve time from API in a format like this : 2020-08-31T15:05:10.768904Z and I'm trying to compare this date with the current Date().
What is the best way to calculate difference between the two.
Thanks!
Sorry, that's not in comment because I cant yet. You have to decompose it and i would use regex to get it.

Firebase Timestamp returns with wrong date - Javascript

I'm trying to get the Timestamp value from firestore (using Firebase Functions), and I´ve successfully done it localy with the toDate() method of Timestamp, and moment library.
moment(doc.data().EndDate.toDate())
But when I deploy my code to firebase and test the function, somehow the toDate() returns a Date with 1 less hour than the saved timestamp on firebase. I suppose it is transforming my date to UTC, since I'm in UTC+1, and the Timestamp is also stored with UTC+1 in firestore, but I don't know how to reliably get the timestamp date as is in firestore, regardless of timezones.
If someone knows why this happens or has any idea how to solve it it would be great.
All timestamps in Firestore are stored in UTC. If you see something different in the Firebase console, that's just your browser formatting it for your local timezone.
In JavaScript, all Date objects are also represented in UTC. If you format that as a string, you will again possibly get a different representation based on your local timezone.
If you write code that computes values using dates or timestamps, you should perform all your computations using UTC. This is the pretty much all computing systems want to deal with dates. When it comes time to format the date for display to an end user, only then should you take timezone into account, and present something according to the user's preferences.

MongoDB current date in save method

How to make MongoDB fill a field with current UTC DateTime on save method?
I know there is $currentDate operator, but it's only available in update method.
It sounds like you're trying to get access to the time on the mongo server for the insert, right? Mongo _ids encode the timestamp of their creation -- would that work for your needs? https://docs.mongodb.com/manual/reference/method/ObjectId/
Otherwise, I think you might need to provide a date from your application code.

Is it ok to store moment js object in redux react?

In redux state tree How do we store dates ?
store moment objects in redux
store js date objects in redux
convert to ISO 8601 string and then store
Something better than these
ie,
state={
fromTime: action.payload.fromTimeMoment,
dateTime: action.payload.dateTimeEs6,
toTime: action.payload.toTimeAsString,
}
In our current project we have stored it as moment objects, still not sure if that was the right thing to do.
When dealing with dates I'd prefer to store as a unix format or ISO string. The reason for this is because I might want to save the state to the local storage for speed things up for second time users.
Keeping instances of other classes (or in this case moment) in the store, you might find some problems while serializing the state.
Using reselect you can create a moment instance to access the date value.
i would just format it to epoch or the ISO string you mentioned and store it, and if its needed again convert it back to whatever you need. its pretty redundant to store an object over and over

Update couch db docment date format

Is there way I can update all the date field format existing in documents of couch db
change format from
DateTime : "07-29-2017 19:07:23"
to
DateTime : "2017-07-29 19:07:23"
There's no automated way to do this, aside from writing a script that updates each of your documents.
An alternative, depending on your exact situation, might be to use a view to manipulate the data as it's being read. The view could detect the existing date format, and if it's the old one, convert it before displaying the document.
This would change the way you query the data, though--you'd have to request the new view, which could obviously be a deal-breaker in some scenarios.
First, you need a way to apply a function to every document. To do so, I suggest that you use pouchdb-migrate.
Finally, you only have to define your function and integrate it into pouchdb-migrate. You can either parse the date and convert it or simply do some string manipulation.

Categories