0

I have moment as a library within my project so answers with moment are fully welcomed.

I have this date 17th May 2017, I need to convert this into a valid date object. How can I achieve this?

Both of the following returns Invalid Date

moment('17th May 2017').format(...)
new Date('17th May 2017')

What's odd is that I can use moment to format to 'Do MMMM YYYY' which outputs the date like the above, however I am able to do it vice versa.

Update

Please see this JSFiddle where it is working.

However, when I port it into my React app, it doesn't work as intended

const {
  safe_date: date,
} = firstVideo;
console.log(
  'MomentJS me!',
  date,
  moment(date, 'dd MMM yyyy'),
  moment(date, 'dd MMM yyyy').valueOf(),
);

date is a String

The output of my console.log

MomentJS me! 17th May 2017 ... NaN

I am not interested in the console.log of moment(date.toString(), 'dd MMM yyyy') so I have omitted it, the odd thing is the valueOf() is returning NaN but returns correctly on JSFiddle.

RobG
  • 142,382
  • 31
  • 172
  • 209
Dan
  • 8,041
  • 8
  • 41
  • 72
  • 2
    `moment('17th May 2017', "dd MMM yyyy")` I guess. you have to specify the format as well in which you feed value – Rajesh May 19 '17 at 13:18
  • I am ready to facepalm, thank you so much. If you provide an answer I shall upvote and accept. – Dan May 19 '17 at 13:19
  • 1
    you have to tell moment what format(s) to expect as there are literally thousands of possibilities ... the moment library is a lot of things, but it doesn't "know what you want" – Jaromanda X May 19 '17 at 13:19
  • I'm glad i was able to help you. I guess there are few posts already serving the answer. I'd mark it dupe. So if you have got satisfactory answer, you can remove post – Rajesh May 19 '17 at 13:20
  • @Rajesh I have updated my question with another brief question. – Dan May 19 '17 at 13:36
  • Upgraded moment to 2.5.1 and it worked. – Dan May 19 '17 at 13:43
  • Possible duplicate of [Parse DateTime string in JavaScript](http://stackoverflow.com/questions/1576753/parse-datetime-string-in-javascript) – Heretic Monkey May 19 '17 at 13:44
  • @Rajesh—one reason to not put answers in comments is that it's difficult to address them directly. The correct [*parse format string*](https://momentjs.com/docs/#/parsing/string-format/) to use is "Do MMMM YYYY", where "Do" means date with ordinal, like 1st, 2nd, etc. Moment.js uses (mostly) upper case tokens for the date parts and (mostly) lower for the time parts. – RobG May 19 '17 at 22:45
  • @RobG I'll keep a note. Also reason for comment was that i was not sure if the format. So gave a hint to OP and hoped he/she would find the correct format. – Rajesh May 20 '17 at 02:50

1 Answers1

-2

Try moment('17th May 2017', "dd'th' MMM yyyy"). I tried in java working properly

SimpleDateFormat outputFormat = new SimpleDateFormat("dd'th' MMM yyyy");
System.out.println(outputFormat.parse("17th May 2017"));
  • The language is ECMAScript, the parser is Moment.js, the format string should be "Do MMMM YYYY". :-( – RobG May 19 '17 at 22:50