.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'
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 ?
Most helpful comment
See the "Default format" section at the bottom of the format documentation. It works like this: