solaire
solaire

Reputation: 505

Set input-timezone in moment js

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)

Upvotes: 4

Views: 8957

Answers (2)

Madhan Varadhodiyil
Madhan Varadhodiyil

Reputation: 2116

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 :)

Upvotes: 3

solaire
solaire

Reputation: 505

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

Upvotes: 2

Related Questions