admin管理员组

文章数量:1415420

I am setting the locale in momentjs using :

moment.locale('en-GB');

but every time I create a new object I have to use a format string, e.g.:

moment('12/01/2001','DD/MM/YYYY');

Is it possible for me to default moment so that it uses dd-mm-yyy everywhere so I only have to use:

moment('12/01/2001');

I am setting the locale in momentjs using :

moment.locale('en-GB');

but every time I create a new object I have to use a format string, e.g.:

moment('12/01/2001','DD/MM/YYYY');

Is it possible for me to default moment so that it uses dd-mm-yyy everywhere so I only have to use:

moment('12/01/2001');
Share Improve this question asked Nov 5, 2014 at 20:03 SturmUndDrangSturmUndDrang 1,9767 gold badges29 silver badges47 bronze badges 2
  • github./moment/moment/issues/308 – Andreas Commented Nov 5, 2014 at 20:08
  • Per the thread linked by @Andreas' ment, it seems like "no". – maerics Commented Nov 5, 2014 at 20:16
Add a ment  | 

3 Answers 3

Reset to default 4

As pointed out in the ments this seems not to work. You can provide a wrapper function though:

function localeMoment(date){
    return moment(date, 'DD/MM/YYYY');
}

This is probably the closest that you can get.

You can wrap the @k-nut function in a more elegant way

String.prototype.toMomentDate = function () {
    return moment(this, 'DD/MM/YYYY');
}

and call it on your strings which store dates

var date = '12/01/2001';
date.toMomentDate();

You can use

moment('12/01/2001','L');

"L" will automatically set the current format depend on locale

本文标签: javascriptDefault momentjs to ddmmyyyy formatStack Overflow