1

I have the following date =>

October 16 at 6:24 PM

I would like to parse this date into a Javascript Date Object using dateFNS

I tried to follow the doc and did

dateFns.parse(stringDate, 'MMMM d [at] h[:]mm a..aaa', new Date())

but it always return me Invalid Date

https://stackblitz.com/edit/typescript-ts9uub?file=index.ts

am I using it wrong ?

7
  • 1
    a..aaa? what's that? Commented Oct 27, 2020 at 5:22
  • I think that means a or aa or aaa - I couldn't make your code work for anything without a year ... until I used the latest version of date-fns (i.e. 2.16.1 as opposed to 1.29.0 that you're using) Commented Oct 27, 2020 at 5:34
  • 1
    it looks like you're trying to use v2.x parse method, but loading 1.x version of datefns - parse did not look like that at all in 1.x Commented Oct 27, 2020 at 5:38
  • @JaromandaX date-fns.org/v2.16.1/docs/format its supposed to be AM or PM Commented Oct 27, 2020 at 6:04
  • I am using npm version 2.16.1 maybe the fiddle is outdated I will check Commented Oct 27, 2020 at 6:04

1 Answer 1

2

use

parsedDate = parse(stringDate, "MMMM d 'at' h':'mm a", new Date()); 

The documentation states that for AM/PM use a..aaa ... this actually means use 1, 2 or 3 a for AM/PM ... then there's aaaa and even aaaaa I think ...

Also [at] should be 'at' etc

Sign up to request clarification or add additional context in comments.

3 Comments

and the a..aaa
It would be good if this answer could be expanded to say when you might want to use 1, 2 or 3 as - i.e. does a do something different to aa?
The documentation and this answer is clear @Dezza a, aa and aaa produce AM or PM - you can use 1, 2 or 3 a and produce the same result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.