Java / JavaScript Dates: Is it True?

So, let's say a user launches my web application from his browser in a different time zone than the application server. I am serializing the date on the client side using the date.getTime() JavaScript method. I send the received milliseconds via Json and then create a server-side Java Date object by calling new Date(millisecondsFromJS) . I save this to MySql, retrieve it, serialize it again, calling Java date.getTime() and sending it again to the client via Json.

If I create a JavaScript Date object with these milliseconds, will this lead to the original date? I have successfully completed this process, but the client and server are currently in the same time zone. I'm not sure if the date will be damaged in the process if the time zones are different.

As I understand it, using getTime () returns a point in time that is independent of time zones. If the user captured the CDT on July 17, 2012 at 4:39 pm, the server can save it as July 17, 2012 at 11:39 pm CEST, for example, but as soon as the server converts this to milliseconds from GMT, and the client creates a date from these milliseconds , he would have successfully restored the original on July 17, 2012 at 4:39 pm CDT. It's true?

+8
java javascript date
source share
2 answers

This article was a good tip. Lessons learned from Dropbox, Part 1 :

Store everything in UTC inside ! Server time, material in the database, etc. This will save a lot of headaches, not just summer time. Some software simply does not process time other than UTC, so do not do this! We saved the clock on the wall set to UTC. When you want to display the time for the user, do the conversion of the time zone to the last second.


Send milliseconds of unix time to the server, and you know what point in time the user has selected. Then process everything on your server in UTC and return the millisecond integer back to the client.

Client / JavaScript:

 var date = new Date(); var clientMilliseconds = date.getTime(); // send clientMilliseconds to server 

Server / Java:

 Date date = new Date(clientMilliseconds); // store the date, then get it back long serverMilliseconds = date.getTime(); // send serverMilliseconds back to client 

Client / JavaScript:

 var date = new Date(serverMilliseconds); // If receiving the error "Invalid Date", serverMilliseconds // needs to be converted to an Integer. Consider: // parseInt: parseInt(serverMilliseconds, 10) // unary +: (+serverMilliseconds) 

Along the way, the date objects on the server and on the client will reflect the corresponding time intervals, so if you look at both options, they may seem different, but this is not the case if you convert them back to UTC using the same time zone.


So, to answer your question:

If I create a JavaScript Date object with these milliseconds, will this lead to the original date?

Yes.

The Java Date(long date) constructor and getTime() method work with unix milliseconds. So JavaScript getTime() and date constructor . There should be no other time zone than Coordinated Universal Time (GMT / UTC).

+9
source share

There are some problems

  • The client is in a different time zone. This is not a problem if you use .getTime() , .valueOf() etc., because they use ms from the era according to UTC.
  • The real problem is that the client’s clock may be skewed. If someone installed their computer back in time so as not to register some programs, for example, this will cause the client to generate false timestamps
  • They may also have slight asymmetries just because the clock is inaccurate such as

You can counter this by sending a timestamp from your server, and then comparing it with a timestamp created by the client to get the asymmetry offset. Then you apply this asymmetry shift to all new Date.getTime() that you generate on the client. Although this will not work if the client changes his system time when using your page.

+2
source share

All Articles