需要使用moment js格式化时区偏移量 [英] Need to format time zone offset using moment js
问题描述
我的页面上有一个日期选择器,当我选择任何日期时,它会产生类似
I have a datepicker on my page, when I select any date it produced a result something like
Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
我需要格式化它:YYYY-MM-DDTHH:mm:ss Z
因此,为此,我使用矩语法
So, for this I use moment syntax
var date='Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)';
moment(date).format('YYYY-MM-DDTHH:mm:ss Z');
产生输出
2014-09-07T00:00:00 +05:00
那很好,但是我的api希望标准时区偏移量为"Z",而不是我的情况下解析为本地当前时区(即+5:00).
That's good, but my api expect standard timezone offset 'Z' instead of parsing into local current time zone (i.e +5:00) in my case.
所以,我想制作这个
2014-09-07T00:00:00Z
怎么可能?
推荐答案
使用 moment.utc()以UTC时间而不是本地时间显示时间:
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment(date).utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
或moment().toISOString()
以 ISO格式(格式:YYYY-MM-DDTHH:mm:ss.sssZ
,时区始终为UTC)显示字符串):
or moment().toISOString()
to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ
, the timezone is always UTC):
var dateValue = moment(date).toISOString();
这篇关于需要使用moment js格式化时区偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!