Format a string as a date

Hi guys

In jquery, how can I turn a string “7th May 2020” into a date dd/mm/yyyy ?

Thanks

1 Like

jQuery has jQuery UI, and datePicker from there lets you format the date:

$.datepicker.formatDate( "dd/mm/yyyy", new Date("7 May 2020") );
Edit:

Updated to be a valid date string

Thanks. I just tried that and Im getting Nan/Nan/NanNanNan

That’s most likely because the literal date string was copy/pasted from the forum where it was ‘published’ and now contains smart/curly quotes. Delete and retype using straight quotes "

It appears that the Date() parser doesn’t understand the th suffix, but with

new Date('7 May 2020')

it should work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.