Reputation: 664
I have just created a blog script, but when I add a comment.
The timestamp in the database is 2014-07-29 18:55:00
even that the clock on my PC is 2014-07-29 20:55:00
.
So I would like to change the time according to the timezone.
So I tried with this code (where timestamp is 2014-07-29 18:55:00
):
var UTC = moment.tz(timestamp, "UTC");
timestamp = UTC.clone().tz(timezone).format('YYYY/MM/DD H:mm:ss');
Note: window.timezone
is set using the following code:
var tz = jstz.determine();
var timezone = tz.name();
The time I want to get out is 2014/07/29 20:55:00
, but I get this out: 2014/07/29 17:53:47
I am using the following libraries for this above code:
Hope that someone understands whats going on.
NOTE: I live in Europe/Denmark
which have the timezone UTC/GMT +2 hours
Upvotes: 0
Views: 152
Reputation: 241430
In addition to script, moment-timezone requires data to function. If you don't have the data loaded, in version 0.1.x it will use the local time zone - as if you just called moment()
without any tz
specified. This has been fixed for the upcoming 0.2.0 release, such that it gives an error instead of silently doing the wrong thing.
Normally, you could use the data builder to just provide data for the time zones you were interested in. However, it is currently offline because the format of the data changed in the last release and the author has not yet had time to complete the required updates.
In the meantime, you can use one of the "data-included" versions, downloadable at the top of the main page - for example, use moment-timezone-2010-2020.js
instead of just moment-timezone.js
.
Also, you since you're using jsTimeZoneDetect - you should test the value returned by jstz.determine()
to make sure it is indeed the time zone you are expecting. The +2 offsets are notoriously difficult to test for. See some of the known issues.
Upvotes: 2