1

I need a locale-based parser for string to Date. I have explored for solutions (both library or custom) but all I found are either assuming a fixed locale, or Date -> string which is fairly easier.

Example:

parse('1/2/2021', 'en-US') // must print Date object as "Sat Jan 02 2021 00:00:00 GMT+0530"
parse('1/2/2021', 'en-IN') // must print Date object as "Mon Feb 01 2021 00:00:00 GMT+0530"

Note that depending on the locale argument, 1 is parsed as a month (for US locale) or as day (for IN locale)

Anil
  • 329
  • 2
  • 9
  • the GMT+0530 will be your timezone – mplungjan Dec 01 '21 at 13:17
  • Not minding the timezone, but what is parsed as month & day must be dependant on locale. Example, US uses MM/DD/YYYY and India uses DD/MM/YYYY – Anil Dec 01 '21 at 13:19
  • 1
    So does Europe. So why not just use moment as you have tagged and specify the parameter moment.locale('en-US'); or moment.locale('en-IN'); – mplungjan Dec 01 '21 at 13:19
  • Oh I see moment(str).format('DD-MM-YYYY') does that. but how can I define a mapper for locale ('en-US') to format ('MM-DD-YYYY') as my requirement is from locale – Anil Dec 01 '21 at 13:23
  • 1
    See also [Javascript parse date from toLocaleDateString for known locale](https://stackoverflow.com/q/37832938/215552) – Heretic Monkey Dec 01 '21 at 13:29
  • This post mentioned as duplicate, answers my question well. Thank you v much folks. https://stackoverflow.com/a/56480865/6723462 – Anil Dec 01 '21 at 13:31

0 Answers0