umbe1987
umbe1987

Reputation: 3568

momentjs - why is moment([2020, 0, 29, 0, 0, 0]).utcOffset(0); showing as '2020-01-28T23:00.00+00:00'?

I am trying to create a time object which needs to represent this time: 29 January 2020 00:00:00.

I need to work without time zone in myproject and so I created my moment object like so:

let m = moment([2020, 0, 29, 0, 0, 0]).utcOffset(0);

When I log the date I created to the console, this is what happens:

console.log(m.format('YYYY-MM-DDTHH:mm:ssZ');
>>> 2020-01-28T23:00.00+00:00

Is it an issue with '00:00:00' time? Why do I have 23:00:00 instead of the desires dvalue?

Upvotes: 0

Views: 59

Answers (1)

VLAZ
VLAZ

Reputation: 28970

According to the Moment.js documentation:

Parse

[...]

Array

[...] Construction with an array will create a date in the current time zone. To create a date from an array at UTC, use moment.utc(Number[]).

Therefore [2020, 0, 29, 0, 0, 0] is interpreted as your local timezone. Calling utcOffset(0) then shifts that number back one hour, thus you're currently in a +01:00 timezone.

As the documentation suggests, use the moment.utc(Number[]) method to create a date in UTC timezone directly:

let m = moment.utc([2020, 0, 29, 0, 0, 0]);

console.log(m.format('YYYY-MM-DDTHH:mm:ssZ'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.28.0/moment.min.js"></script>

Upvotes: 1

Related Questions