简体   繁体   中英

Time not working as expected using moment.js

I am working on a web application where i want to find the local time for different time zones. I am using moment.js in order to accomplish this task. The problem is that returning time does not work as expected. For example:

moment().tz("Europe/London")._d;

will return Thu Aug 15 2013 01:22:23 GMT+0300 (GTB Daylight Time) but at the same time local hour at London is Thu Aug 14 2013 22:22:23 so there is a 3 hour difference. Can anyone explain why this is happening and suggest a solution? Thank you.

Don't use the ._d property. The underscore is there to indicate that it's internal. (Since that is a Date , it will always show as if it were in the local time zone, because that's how JavaScript works.)

Instead use one of the output methods, such as .format() .

var time= moment.tz(value).format('LTS'),
var date= moment.tz(value).format('LL'),
var abbr= moment.tz.zone(value).abbr(moment.utc().valueOf()), //PST,IST
var name= moment.tz.zone(value).name); //Asia/Kolkata

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM