如何使用 moment.js 自动转换用户时区
How to auto convert on user TimeZone using moment.js
我有这个格式的日期:
周日20:30America/Toronto
我希望每次用户访问带有日期的页面时都能自动时区转换该日期,但我只想转换并显示例如星期日这种格式 20:30 .
提前致谢。
您可以使用 moment-timezone alongside with timeZoneDetect 这将获得所需的行为。
例如:
var tz = jstz.determine();
var userTimeZone = tz.name();
//you can use any date format here you want
var currentTime = moment.tz("2014-06-01 12:00", "America/Toronto"); // time defined in Toronto timezone
var userTime = newYork.clone().tz(userTimeZone);
我有这个格式的日期:
周日20:30America/Toronto
我希望每次用户访问带有日期的页面时都能自动时区转换该日期,但我只想转换并显示例如星期日这种格式 20:30 .
提前致谢。
您可以使用 moment-timezone alongside with timeZoneDetect 这将获得所需的行为。
例如:
var tz = jstz.determine();
var userTimeZone = tz.name();
//you can use any date format here you want
var currentTime = moment.tz("2014-06-01 12:00", "America/Toronto"); // time defined in Toronto timezone
var userTime = newYork.clone().tz(userTimeZone);