开发者

Javascript timezone solution needed(taking into account the actual difference in UTC timestamps)

I have unix timestamps from time zone X which is not known,

the current timestamp(now()) in TZ X is known 1275143019,

how to approach a javascript function so that it can generate the datetime in the users current TZ in the format 2010-05-29 15:32:35 ?

UPDATE

I'm not a unix timestamp expert,if unix timestamp is always the same in different TZ,

then I have to change the question a little,so that the current datetime in TZ X is known(like 2010-05-29 22:32:28),and the other datetime is also in this format,how to convert them to the user's TZ based on the difference between now() ?

UPDATE

Something strange from MySQL:

On server:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 18:34:30 | 
+---------------------+
1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1275143674 | 
+------------------+
1 row in set (0.00 sec)

On local:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 22:41:30 |
+-------------开发者_开发知识库--------+
1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1275144091 |
+------------------+
1 row in set (0.00 sec)

Why the difference between now() (2010-05-29 22:41:30-2010-05-29 18:34:30=6hours) and UNIX_TIMESTAMP() (1275144091 - 1275143674 = 417seconds) is not the same ?


When you instantiate a Date object, you can pass in a Unix-style "milliseconds since the epoch" time value:

var date = new Date(milliseconds);

That time value is interpreted as UTC. When you call toString on that date object, or use the getters for hour/minute/second, you get values in the local timezone at the client.

As noted by @vava in a comment, the milliseconds or seconds current time value you get back from Unix/Linux is always UTC to begin with, so your server can drop that value into a page and when it executes on the client, the right thing will happen.

edit — If you need to convert from local time on the server to local time at the client, you really need to get your server time in UTC, or at least know the UTC offset. From year/month/day hour/minute/second values you can set up a Javascript date with the "setUTCFoo" functions (where "Foo" if "FullYear", "Month", "Date", "Hour", "Minute", "Seconds", "Milliseconds").

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date


The difference between the select now(); commands is because of the timezone difference. The select UNIX_TIMESTAMP(); should be always the same on any two machines.

Why is then the 417 seconds differene? It is simple:

417 seconds ~= 7 minutes

22:41:30 - 18:34:30 = 7 minutes

Synchronize the clocks and the unix timestamps will be the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜