我有这些代码行
const startTime = '2017-09-12T09:00:00-04:00';
让 s = 时刻(开始时间);
s.format()
这些行输出 '2017-09-12T13:00:00+00:00'
我怎样才能得到与之前字符串相同的输出? '2017-09-12T09:00:00-04:00'
在查看了 stackoverflow 上的一些其他答案后,我尝试阅读文档并尝试使用 utcOffset()
s.utc().format()
我也按时执行添加操作,但在“此刻”似乎不相关。
最佳答案
var startTime = '2017-09-12T09:00:00-04:00';
var s = moment.parseZone(startTime);
s = s.add(1, 'd');
console.log(s.format()); //2017-09-13T09:00:00-04:00
startTime = '2017-09-12T09:00:00+00:00';
s = moment.parseZone(startTime);
console.log(s.format('YYYY-MM-DDTHH:mm:ssZ'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Moment's string parsing functions like moment(string) and moment.utc(string) accept offset information if provided, but convert the resulting Moment object to local or UTC time.
In contrast, moment.parseZone() parses the string but keeps the resulting Moment object in a fixed-offset timezone with the provided offset in the string.
关于javascript - Momentjs 更改时区值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46208374/