Moment: option to set default output for `.format()`

Created on 22 Jul 2016  ·  2Comments  ·  Source: moment/moment

.format() will output timezone relative WITHOUT milliseconds EG:

> moment(1469141760123).format()
'2016-07-21T15:56:00-07:00'

.toISOString() will output UTC WITH milliseconds

> moment(1469141760123).toISOString()
'2016-07-21T22:56:00.123Z'

.format('YYYY-MM-DDTHH:mm:ss.SSSZ') will output timezone relative WITH milliseconds

> moment(1469141760123).format('YYYY-MM-DDTHH:mm:ss.SSSZ')
'2016-07-21T15:56:00.123-07:00'

I find the default timezone relative without milliseconds pretty useless, but don't think I should need to write out the full format every time just to include it. It would be nice to be able to set the default format output:

// set the default
moment.setDefaultFormat('YYYY-MM-DDTHH:mm:ss.SSSZ');

// or
moment.format.setDefault('YYYY-MM-DDTHH:mm:ss.SSSZ');

moment(1469141760123).format(); // '2016-07-21T15:56:00.123-07:00'

Most helpful comment

See the "Default format" section at the bottom of the format documentation. It works like this:

> moment.defaultFormat
'YYYY-MM-DDTHH:mm:ssZ'
> moment.defaultFormatUtc
'YYYY-MM-DDTHH:mm:ss[Z]'
> moment.defaultFormat = "YYYY"
'YYYY'
> moment().format()
'2016'

All 2 comments

See the "Default format" section at the bottom of the format documentation. It works like this:

> moment.defaultFormat
'YYYY-MM-DDTHH:mm:ssZ'
> moment.defaultFormatUtc
'YYYY-MM-DDTHH:mm:ss[Z]'
> moment.defaultFormat = "YYYY"
'YYYY'
> moment().format()
'2016'

moment.defaultFormat is a readOnly property and cant set it. How to set defaultFormat ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments

nikocraft picture nikocraft  ·  3Comments

slavafomin picture slavafomin  ·  3Comments

M-Zuber picture M-Zuber  ·  3Comments

benhathaway picture benhathaway  ·  3Comments