最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set input-timezone in moment js - Stack Overflow

programmeradmin3浏览0评论

I have an input on my website and my timezone is utc+2h:

var startDate = "01/01/2019"
result = moment(startDate).utc().format();
alert(result);

This gives back 2018-12-31T23:00:00+00:00, but what I need for the api is 2019-01-01T00:00:00+00:00 (first of month at utc timezone), so what i need is more or less set the input timezone to already be utc. Is there a way to do this with Moment.js? (else I could do the whole thing manually of course)

I have an input on my website and my timezone is utc+2h:

var startDate = "01/01/2019"
result = moment(startDate).utc().format();
alert(result);

This gives back 2018-12-31T23:00:00+00:00, but what I need for the api is 2019-01-01T00:00:00+00:00 (first of month at utc timezone), so what i need is more or less set the input timezone to already be utc. Is there a way to do this with Moment.js? (else I could do the whole thing manually of course)

Share Improve this question asked Aug 12, 2018 at 8:25 solairesolaire 5055 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use moment timezone

You can set default time zone to UTC ref

By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node.js server.

To change the default time zone, use moment.tz.setDefault with a valid time zone.

moment.tz.setDefault("America/New_York");

OR

You can explicitly set timezone, when parsing date:

m = moment.tz("2013-11-18 11:55", "Europe/Berlin");

And convert it to timezone you require later, like:

m.tz("UTC");

Hope this helps :)

This seems to give me the right result:

var startDate = "01/01/2019"
result = moment.utc(startDate, "DD-MM-YYYY").format();
alert(result);
//2019-01-01T00:00:00+00:00
发布评论

评论列表(0)

  1. 暂无评论