Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment.js - change format of already formatted past date

I need to get a past date with moment.js (http://momentjs.com/) which I receive in specific format:

moment('31.10.2013', 'dd.mm.yy');

That returns me a strange response where I see a current date in _d option:

// returns
_a: Array[7]
_d: Wed Nov 06 2013 00:10:00 GMT+0200 (EET)
_f: "dd.mm.yy"
_i: "26.10.2013"
_isUTC: false
_l: undefined
_pf: Object
_strict: undefined

I assume that is the problem when I do formatting:

moment('31.10.2013', 'dd.mm.yy').format('YYYY/MM/DD');
// returns current date (why??!)
// "2013/11/06"

So what is wrong here, could I format the past date?

like image 445
Kosmetika Avatar asked Nov 06 '13 08:11

Kosmetika


People also ask

How can I get date in dd mm yyyy format in moment?

SearchDate = moment(new Date(), "DD/MM/YYYY");

Is MomentJS deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.


1 Answers

At first I thought it was the format, but it was actually the format in, lower case means something different than uppercase both in and out.

moment('31.10.2013', 'DD.MM.YYYY').format('YYYY/MM/DD')
>> "2013/10/31"

moment('31.10.2013', 'dd.mm.yyyy').format('YYYY/MM/DD')
>> "2013/11/06"

So fix your input mask, not the format.

like image 128
Ted Johnson Avatar answered Nov 15 '22 12:11

Ted Johnson