0

I'm not sure if this is going to be a duplicated question as I couldn't find anything on SO therefore, I'm going ahead with this question -

I have a date string which is locale-dependent and I have the locale info with it too.

Eg. dateStr = '06/07/2021' and locale='en-GB'.

How do I get a JS Date object from this? The Date constructor doesn't seem to take a locale and by default parses it with respect to the en-US locale(MM-DD-YYYY).

By this I mean, that the above dateStr will be converted to 7th June 2021 and not the actual 6 July 2021 using the Date constructor.

UPDATE:
I got something from d2l-intl but it doesn't work. Quite strange.

var parser = new d2lIntl.DateTimeParse('en-GB');
var date = parser.parseDate('23/05/2021');
console.log(
    date.getMonth(),
    date.getDate()
);

which breaks as it still accepts date string in en-US format.

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
  • 1
    Some discussion here: [Javascript Date Parse with specific locale](https://stackoverflow.com/questions/50781887/javascript-date-parse-with-specific-locale) – pilchard Sep 11 '21 at 11:45
  • Have a look at JavaScript's `Intl` library. All the major browsers support it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl – Dai Sep 11 '21 at 11:48
  • @Dai, the Intl object is great for going from Date to locale, but doesn't offer help going the other way. – pilchard Sep 11 '21 at 11:50
  • @pilchard I don't really want to write a map object between locales and their formats. Isn't something as 1900 as this available with some library where I can just pass the date string and the locale and I get a Date object to play with? – Aakash Verma Sep 11 '21 at 11:53
  • 1
    If you want a library then [date-fns](https://date-fns.org/) or [moment](https://momentjs.com/) – pilchard Sep 11 '21 at 11:55
  • "en-GB" is a **language**. – RobG Sep 11 '21 at 12:40
  • 1
    Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Heretic Monkey Sep 11 '21 at 12:40
  • 1
    `Date` objects do not have a language (or locale) associated with them. They are stored as milliseconds since a specific point in time in a specific place. From that, that same point in time in other places can be calculated and formatted to the standards associated with various languages and regions (as @Dai alluded to in their comment, and as several answers in the linked post demonstrate). – Heretic Monkey Sep 11 '21 at 12:52
  • 2
    Why use a language code to determine the format when there are formats that don't map to any language (e.g. various ISO 8601 formats) and you can pass the actual format, e.g. instead of "en-GB" pass "d/m/y" or similar. There are many different token schemes in use that can be borrowed from. – RobG Sep 11 '21 at 12:54
  • Ask the backender to store in UTC format. That's basic for SQL. If it's frontend that sends in locale and local timestamps --- don't! Get and store in UTC format, but format the date to locale for the user. The problem isn't what you're asking for, but what generates the problem in the first place. – Rickard Elimää Sep 11 '21 at 13:13
  • @RobG en-GB is a *locale*, en is a language. – Aakash Verma Sep 11 '21 at 15:47
  • 1
    @AakashVerma How do you get the dateStr and and your assumption of locale? What code do you use to get it? Your post lacks details. – Rickard Elimää Sep 11 '21 at 16:57
  • @HereticMonkey I am not asking for the Date object to hold the locale but to parse the Date string correctly given the locale. Basically take the month as month and the day as day and not vice versa. – Aakash Verma Sep 12 '21 at 03:51
  • @AakashVerma—IETF BCP 47 language tags allow for a number of subtags. *en* is a language, *gb* is a country code that identifies a particular variant of English. *en-GB* does not in any way identify a geographic location (i.e. locale). Only ECMA–402 seems confused about that. – RobG Sep 12 '21 at 07:42
  • 2
    `Date` objects are not responsible for parsing any strings other than those specified in the ECMAScript standard. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/215552). See the link I posted for the many ways people have found for working with what JS provides. – Heretic Monkey Sep 12 '21 at 17:36
  • @HereticMonkey I can see why you could have misinterpreted my comment. I don't mean for the Date object to do anything. I mean for the library to create a date object based on the locale and locale formatted date string that is passed to some function made for this purpose. – Aakash Verma Sep 13 '21 at 10:41
  • @RickardElimää I think you love to suggest answers out of the scope of the problem. Assuming that I have control over the inputs or the backend is not the correct way to go. – Aakash Verma Sep 13 '21 at 10:43
  • If you're looking for a library, Stack Overflow is not the place to ask for that. See the [help/on-topic], "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." Again, the link I posted (and many, many other questions about date parsing on Stack Overflow) have links to libraries. – Heretic Monkey Sep 13 '21 at 12:41
  • @HereticMonkey I never asked for a library in my original question. In fact, I am asking people to just not suggest the library as it's not helpful enough. The number of unnecessarily triggered people who aren't providing a solution even is high. – Aakash Verma Sep 16 '21 at 07:47
  • @RobG Why do I care if ECMA-402 is confused about it? Is that something I can change when I working with Javascript? No! So in the scope of my question, locale is correct. Btw even for java env, en-GB is a locale. – Aakash Verma Sep 16 '21 at 07:56
  • @AakashVerma—the International Engineering Task Force (IETF) defines language tags, they're called "language tags" because they identify languages. ECMA-402 uses BCP 47 language tags to define the language to use for formatting dates, the *parameter* name is "locale" and various methods include the word "locale" because the intention is to create a localised version of a value (number, currency, date, etc.). That doesn't mean the BCP 47 language tag is a locale, it isn't. It's a language tag to be used for localisation. – RobG Sep 16 '21 at 10:44
  • @RobG en_GB is a *locale* https://ogp.me/#types and https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html – Aakash Verma Oct 21 '21 at 11:24

1 Answers1

1

Looking at the question's comments, I believe sometimes people just don't understand the question and start blaming the problem itself. It's ridiculous :D

This may not be the perfect way to go about it (there can be something cleaner and shorter), but this definitely works.

locale = 'en-GB';
value = '07/06/2021';


moment.locale(locale);
const localeData = moment.localeData();
const format = localeData.longDateFormat('L');
console.log(moment(value, format).format('YYYY-MM-DD')); // '2021-06-07'
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66