2

Using moment js

var y = moment('2016-01-11T16:00:00');
   console.log('UTC ' + moment().utc().format());
   console.log('Local ' + moment().format());
   console.log(y.format());
   console.log(y.toISOString());

Above code outputs

UTC 2016-07-12T19:54:15Z
Local 2016-07-12T15:54:15-04:00
2016-01-11T16:00:00-05:00
2016-01-11T21:00:00.000Z

Why it is displaying -05:00 offset in y.format().

3
  • Probably your timezone is -05:00. Commented Jul 12, 2016 at 20:11
  • 1
    Because you're not telling it what format you want, so it's giving you the default. See: momentjs.com/docs/#/displaying/format Commented Jul 12, 2016 at 20:11
  • @Gothdo it is -4:00 (EST) Commented Jul 13, 2016 at 1:30

2 Answers 2

1

.format() is designed to take a parameter that allows you to format the date exactly how you want to display it. Since you don't pass a parameter, it gives you the default, which in this case, shows the timezone (-05:00). Per the docs,

As of version 1.5.0, calling moment#format without a format will default to moment.defaultFormat. Out of the box, moment.defaultFormat is the ISO8601 format YYYY-MM-DDTHH:mm:ssZ.

As of version 2.13.0, when in UTC mode, the default format will return Z as the offset, instead of +00:00.

Check the link for info about formatting a date with .format().

Sign up to request clarification or add additional context in comments.

2 Comments

I should have been more clear. When moment().format() prints 2016-07-12T15:54:15-04:00, I understand it defaults to ISO8601 but moment('2016-01-11T16:00:00').format() prints 2016-01-11T16:00:00-05:00 why it is -5 not -4 (It is EST -4 is correct offset). Does it mean it defaults to -5 offset always irrespective of local timezone? or does it not consider daylight saving in which -5 makes sense
did not notice it is JAN 11. so -5 makes sense.
0

moment will default to ISO 8601 (Local time with offset to UTC), have a look a the documentation at moment docs, and more on the standard itself at ISO 8601 Standard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.