I am trying to understand how creation of dates using Javascript on client and server both can affect query results when timezone of client and server can differ.
On client side, I am using Date.now()
to save timestamp information
var time = Date.now()
console.log(time);
The now() method returns the milliseconds elapsed since 1 January 1970 00:00:00 UTC up until now as a Number.
My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.
We can also get this timestamp using the following code
var now = new Date();
time = now.getTime();
console.log(time);
This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?
Now, constructing date from Date.now()
and displaying it in browser's console.
var time = Date.now()
console.log(time);
var date = new Date(time)
var time = date.getTime()
console.log(time);
console.log(date);
In my opinion, it is only when displaying in browser's console, date is formatted in the browser's local timezone otherwise this date instance still holds timestamp at zero timezone offset. Is it correct?.
I have also seen an answer of converting Javascript date to UTC
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
time = now_utc.getTime();
console.log(time);
This code still gives the same timestamp information as given by Date.now()
and console.log
statements were differing by few milliseconds only which I think is due to code execution time instead of any timezone difference. When executed, following was the output for that instance of time:
1468408292575
1468388492000
Is there any difference between a UTC timestamp and timestamp given by Date.now()
?
Can you please confirm if these three options produce same result (i.e. independent of any timezone) and if it is safe to use this timestamp information for client and server interactions when they are in a different timezone. Examples:
I want to reduce this to a level where I want to show timezone information only for display purpose e.g. in HTML, in generated files.
My questions might be too simple because I am confused on seeing the Date class documentation on correctly using it to avoid time difference issues.
I posted an answer on date and timezone related question. Answer says that the internal recording of the date generated on client side are at zero timezone offset. Just adding answers to individual questions:
My understanding is that this timestamp is independent of any timezone offset, in other words it points to time at Zero timezone offset. Is it correct?.
Yes
This code still gives the same timestamp information (at zero timezone offset). Is Date.now() just a quick way to get timestamp?
Yes
Is there any difference between a UTC timestamp and timestamp given by Date.now()?
No
both are typically same, but Date.now()
is little faster
Further Reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With