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?
SearchDate = moment(new Date(), "DD/MM/YYYY");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With