Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to set default output for .format() #3320

Closed
ilovett opened this issue Jul 22, 2016 · 3 comments
Closed

option to set default output for .format() #3320

ilovett opened this issue Jul 22, 2016 · 3 comments

Comments

@ilovett
Copy link

ilovett commented Jul 22, 2016

.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'
@icambron
Copy link
Member

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'

@temaisgod
Copy link

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

@Art3miX
Copy link

Art3miX commented Mar 29, 2021

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

import moment from 'moment';

moment.defaultFormat = 'MM / YYYY';
moment.defaultFormatUtc = 'MM / YYYY';

Seems to work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants