Problem analyzing ordinals using Moment JS. Error?

I am just trying to go from moment to string and then go back to moment .

But this does not work:

 f = 'dddd MMMM Do, YYYY' s = 'Friday May 3rd, 2013' d = moment().format(f) # Sunday April 21st, 2013 moment(d, f).format(f) # Monday April 1st, 2013 moment(s, f) # Friday May 3rd, 2013 .format(f) # Wednesday May 1st, 2013 

He always parses the serial number on the first day of the month.

Is there something wrong with my formatting? Or how do I expect format() to work?

+4
source share
1 answer

This issue was raised some time ago here .

The solution is to ignore the literal portion of ordinal dates when parsing, using DD instead of Do

 var f1 = 'dddd MMMM Do, YYYY'; var f2 = 'dddd MMMM DD, YYYY'; var a = moment().format(f1); console.log(a); // Thursday June 20th, 2013 var b = moment(a, f1).format(f1); console.log(b); // Saturday June 1st, 2013 var c = moment(a, f2).format(f1); console.log(c); // Thursday June 20th, 2013 

As you can see, the result of b is the first month because it does not understand what to do with the day number. But c took 20 and ignored the extra th characters, giving the correct result.

+2
source

All Articles