use*_*710 4 javascript timezone momentjs
我必须以这种格式在网页上显示一个字符串: 16:00 HH:mm
我正在使用一个时刻对象来表示日期/时间和时区。
var day = moment().tz('GMT');
day.hours(16).minutes(0).seconds(0).milliseconds(0);
Run Code Online (Sandbox Code Playgroud)
所以这是格林威治标准时间的 16:00。
在我的网页上,我想更改时区,然后收集小时和分钟。
如果我创建一个新的时刻对象
var day2 = moment().tz('PST); //this is 8 AM since gmt was 16
console.log(day2.get('hours'));
Run Code Online (Sandbox Code Playgroud)
是 16 不是 8!
并尝试获取它们在 GMT 而非 PST 中的小时数和分钟数。
我怎样才能在 PST 中获得它?我必须继续包装它吗?
// initialize a new moment object to midnight UTC of the current UTC day
var m1 = moment.utc().startOf('day');
// set the time you desire, in UTC
m1.hours(16).minutes(0);
// clone the existing moment object to create a new one
var m2 = moment(m1); // OR var m2 = m1.clone(); (both do the same thing)
// set the time zone of the new object
m2.tz('America/Los_Angeles');
// format the output for display
console.log(m2.format('HH:mm'));
Run Code Online (Sandbox Code Playgroud)
如果你不能让它工作,那么你没有正确加载时刻、时刻-时区和所需的时区数据。对于数据,您需要moment.tz.add
使用您关心的区域的区域数据进行调用,或者您需要使用站点上可用的 moment-timezone-with-data 文件之一。
在小提琴中,您可以通过展开外部资源部分来查看我正在加载的时刻文件。
归档时间: |
|
查看次数: |
14269 次 |
最近记录: |