| Author |
Passing java dates timestamp to Javascript's date object
|
Fawad Ali
Ranch Hand
Joined: Dec 28, 2009
Posts: 108
|
|
Hi All:
I have a strange problem. I get a timestamp from java date and pass it to a javascript date object. But when I display the date, it shows a plus 10 hours time. Lets say I have a date in Java which is
Sat Aug 20 14:00:00 ACT 2011
and the date.getTime() gives 1313866800000. When I pass this timestamp to javascript date object like below.
it alerts a plus 10 hours date as
Sun Aug 21 2011 00:00:00 GMT+0500 (Pakistan Standard Time)
Any ideas on what I may be doing wrong or should I post it to java forums.
Thanks in advance.
|
Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
new Date(milliseconds)
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
Date MDN Docs
Date.toString() <-- what alert does
Returns the date in a readable string format based on the user's date/time settings.
Eric
|
 |
Fawad Ali
Ranch Hand
Joined: Dec 28, 2009
Posts: 108
|
|
Hi All,
There are two parts to the solution of this problem, but first let me rephrase my problem.
I have certain data against logDate and hour in a data base table. The data looks like below
logDate hr data
2011-09-04 00 12
2011-09-04 01 313
2011-09-04 02 713 .... and so on
I use to get the date from database by rs.getDate and assign it to the java Date object (Here is the first problem, java adds a the server systems timezone offset to it). Then I use the date.getTime() of java to get the time in milli seconds(Remember: this stamp contains the server offset). I paas the stam through json to the javascript and create a javascrip date object by
Here is the second problem, javascript adds the client's timezone offset to it.
The solution to this problem would be to get the date from database, convert it to UTC date and show it as UTC date in the front end.
JAVA SOLUTION:
JAVASCRIPT SOLUTION:
I hope it helps someone else. If I am mistaken in something, please let me know.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
The Java date is likely in UTC. The difference is likely a time zone issue.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
|
|
subject: Passing java dates timestamp to Javascript's date object
|
|
|