Kosmetika
Kosmetika

Reputation: 21304

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?

Upvotes: 14

Views: 24920

Answers (1)

Ted Johnson
Ted Johnson

Reputation: 4343

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.

Upvotes: 34

Related Questions