I'm trying to convert a date object to a Firestore timestamp.
var dateOBJ = new Date();
var timeStamp = new firebase.firestore.Timestamp(dateOBJ);
This gives me an error:
Uncaught Error: Timestamp seconds out of range: Sun Dec 09 2018 11:37:05 GMT+0100
I tried converting the date object to seconds first by using .getTime() / 1000, but it's still out of range.
The timestamp is gonna be the expiration date for an url, so I need to add some time to it.
There are two ways of setting a date field in Cloud Firestore:
You specify a Date value for the field, in which case you fully determine what date is written.
You specify the firebase.firestore.FieldValue.serverTimestamp(), in which case the server writes the current date.
There is no way in the API to combine these two options, you either use one or the other.
Since you comment that you want to store the timestamp and an offset, that is also what I'd store:
a timestamp field that you let the server populate with firebase.firestore.FieldValue.serverTimestamp().
a offset field, that you populate from the app with the offset in days/hours.
That way you can reconstitute the effective expiration timestamp by combining the two fields.
You could even add a third field that stores the expiration timestamp, but that will require an extra write operation. I'd typically do this in Cloud Functions, to ensure you keep full control over the field and clients can't spoof it. If you don't do it in Cloud Functions, consider writing security rules that validate that the value if the calculated field is indeed the result of that calculation.
You won't get a consistent server side timestamp with a JavaScript date. Instead, send the server timestamp from the SDK:
const timestamp = firebase.firestore.FieldValue.serverTimestamp()
If you still want to set the timestamp as a Date you can just pass new Date() to Firestore and it will be saved as a timestamp.
Frank is right about setting timestamps into firestore.
If you want to check that timestamp on the front end afterwards you need to use .toDate on the timestamp object returned from firestore to turn it back into a JS date.
Related
I'm trying to convert my date to the correct time zone with moment.js. However, I always get the whole thing without a time specification.
Here is my program code:
console.log(von);
console.log(bis);
var nVon = moment.tz(von, "Europe/Berlin");
var nBis = moment.tz(bis, "Europe/Berlin");
console.log(nVon.format());
console.log(nBis.format());
This is what I see in the console:
2022-10-31T00:00:00+01:00
And here the original German format, which I want to save in the correct time zone in MongoDb.:
The problem is that it is saved in MongoDB with an hour loss of time like this, without UTC etc.: 2022-10-31T19:44:39.000+00:00
Date values in MongoDB are stored as UTC times - always and only!
If you need to preserve the client input time zone, then you must store it in a separate field. Usually the client takes responsibility to display the MongoDB UTC times as local times.
NB, you should never store date values as string, it's a design flaw. Store always proper Date objects. Thus store
moment.tz(von, "Europe/Berlin").toDate()
I have saved a datetime value created by Luxon into a postgres database column, of type TIMESTAMP(3). I want to then use that value, convert it into other time zones, etc. However, I can't seem to figure out how to "use" it.
I created the object using the following
const { DateTime } = require("luxon");
const myDate = DateTime.now().toUTC().toISO()
I then inserted it into a postgres database, into a column of type TIMESTAMP(3).
I extract it out of the database using a query. When I log it, it says its value is:
console.log(extracted_date); //=> "2021-12-27T09:57:16.184Z"
console.log(typeof extracted_date); //=> object
// The following return "unparsable" or undefined objects
DateTime.fromISO(extracted_date);
DateTime.fromObject(extracted_date);
I can find plenty of tutorials about how to insert dates into sql, but nothing on how to take that date object and actually do something with it. I want to, for instance, convert its time zone.
To use that date object you can initiate a new Date, like so:
console.log(extracted_date); //=> "2021-12-27T09:57:16.184Z"
const javascriptDate = new Date(extracted_date);
Than you can use it directly or with the luxon library.
console.log(javascriptDate.toGMTString()); // => "Mon, 27 Dec 2021 09:57:16 GMT"
console.log(javascriptDate.toISOString()); // => "2021-12-27T09:57:16.184Z"
console.log(javascriptDate.valueOf()); // => 1640599036184
This object core is actually, a value that represents milliseconds since 1 January 1970 UTC, and added to that are time and string parsing functions, generally speaking.
More Info
In some systems dates are store in the database as the value -
date.valueOf() - which make it clearer (for a developer) you have to manipulate it, and perhaps reduce problems of showing the wrong timestamp to users. In the other hand - you lose the readability. Another opion is using only UTC time in your system and convert the timestamp on client side or before presenting the data. The use of UTC will make sure all of your dates will have same language'.
If you want to read more about timestamp,
here are some references:
UTC vs ISO format for time
What is the "right" JSON date format?
Here's my problem : I want to send a timestamp to a firestore document. I was previously using a package called "react-native-datetime-picker", which returned a UNIX Epoch timestamp which I would directly send to firestore using :
startsAt: new firebase.firestore.Timestamp(startsAt, 83000000),
endsAt: new firebase.firestore.Timestamp(endsAt, 83000000),
(startsAt and endsAt being the timestamps). This worked and would send a timestamp to firestore, however, due to the fact that on Android you can only choose either date or time to pick and not both simulteanously, I am now using a packaged named "react-native-modal-datetime-picker" which allows me to pick both. Now this package returns a fully-fletched date in this format :
2021-03-20T18:25:00.000Z
When I try to send this to firestore, it doesn't work while saying : FirebaseError: Timestamp seconds out of range.
So I am guessing that I'm trying to send a non-conventional format of date to Firestore. Is there any way to change this date to a timestamp ? Or is there another workaround for this situation ? Thanks in advance !
I managed to send the date to Firestore using the getTime() function :
startsAt: new firebase.firestore.Timestamp(Math.round(startsAt.getTime() / 1000), 83000000),
getTime() transforms your date into an epoch timestamp in milliseconds, which you divide by 1000 to get in seconds and make sure Firestore stores it as a timestamp. You have to round it, as Timestamp() can't take decimals as parameters.
const timestamp = firebase.firestore.FieldValue.serverTimestamp();
Did you try this method, this is built-in firestore method and work properly across the all timestamps
Reference : https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue
Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.
I would like to know whether the following is the right method to handle datetime data type in WebApi 2, Javascript and database.
DateTime from Javascript to WebApi:
var date = new Date();
var datestring = date.toISOString();
//Send datestring to WebApi
DateTime from WebApi to Javascript:
//on getting datetime value from `http.get` call
var dateFromServer = new Date(dateFromServer);
WebApi:
Incoming date
do nothing simply store the datestring returned in database column with datatype datetime
Getting date from database and Returning date to client:
no datetime manipulation (simply return as per WebApi Json serializer ex: 2015-10-23T18:30:00). Client would automatically convert the UTC datetime to local datetime
Yes if you don't want to handle any information about user Timezone etc... this is an acceptable way.
Just make sure that any time you want a date produced from the server for a comparison or something else to use the c# DateTime.UtcNow
method.
I think Having a "Global UTC Convention" its a quite safe and good solution but it has some limits.
For example if you want to Alert all of your users located in different timezones at 09:00 am (on each user's country) then its impossible to know when its "09:00" for each one.
One way to solve this(and it's the one i prefer), is to store manually each user's timezone info separately on the database, and every time you want to make a comparison simply convert the time.
TimeZoneInfo.ConvertTimeFromUtc(time, this.userTimezone);
Alternatively if you want to store all timezone information on the server you can :
Send your date from javascript to the server using the following format :
"2014-02-01T09:28:56.321-10:00" ISO 8601 also supports time zones by replacing the Z with + or – value for the timezone offset.
Declare your WEB API 2 Date types with the "DateTimeOffset" type.
Finally store your dates within the database using the "datetimeoffset" type.
This way any time on the server or the database you have all the information about the user's time and timezone.
You will find this article useful