I have a date string that is formatted depending on the language/locale of the browser. For example, "03/05/2019"
in en-US
is March 5, 2019 (mm/dd/yyyy) but in en-NZ
, it's May 3, 2019 (dd/mm/yyyy).
I need to know how to convert the string to date using JavaScript with the corresponding locale. I cannot hardcode the format because the locale really depends on the browser. I tried using Moment.js but I cannot seem to make it work. It returns invalid date
for dates like "16/05/2019"
. It looks like it's not following the locale which I set to en-NZ
.
Here is the momentjs code that I have tried:
moment().locale('en-NZ').format('L')
This will return the en-NZ format of the current date which is 27/05/2019 but when I try to include a date string:
moment('16/05/2019').locale('en-NZ').format('L')
This returns 'Invalid Date'. Any suggestions on how to do it or even a different plugin that I can use? Thanks.