Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing java dates timestamp to Javascript's date object

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java date is likely in UTC. The difference is likely a time zone issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic