Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locales can't be used in parsing dates #694

Closed
raketenolli opened this issue Oct 15, 2019 · 8 comments
Closed

Locales can't be used in parsing dates #694

raketenolli opened this issue Oct 15, 2019 · 8 comments

Comments

@raketenolli
Copy link

raketenolli commented Oct 15, 2019

Update: This was originally a bug about missing documentation about using locales without babel.

Describe the bug
I was under the assumption that locales would help in parsing dates.

I tried

const dayjs = require('dayjs');
const enGB = require('dayjs/locale/en-gb');

const dateStringUS = "09/10/2019, 08:51";
const dateStringGB = "11/09/2019, 08:50";

var dateUS = dayjs(dateStringUS).toDate();
dayjs.locale(enGB);
var dateGB = dayjs(dateStringGB).toDate();

console.log("dateUS: " + dateUS);
console.log("dateGB: " + dateGB);

and I get

dateUS: Tue Sep 10 2019 08:51:00 GMT+0200 (GMT+02:00)
dateGB: Sat Nov 09 2019 08:50:00 GMT+0100 (GMT+01:00)

Expected behavior
I was expecting an output like

dateUS: Tue Sep 10 2019 08:51:00 GMT...
dateGB: Wed Sep 11 2019 08:50:00 GMT...

Using dayjs(datestring, { locale: enGB }); yielded the same result.

Information

  • Day.js Version v1.8.16
  • OS: Windows 10
  • Browser node.js v10.15.3
  • Time zone: n/a
@raketenolli
Copy link
Author

So I tried

var dayjs = require('dayjs');
var LocalizedFormat = require('dayjs/plugin/localizedFormat');
var enGB = require('dayjs/locale/en-gb');

const dateStringUS = "09/10/2019, 08:51";
const dateStringGB = "11/09/2019, 08:50";

var dateUS = dayjs(dateStringUS).format("LLLL");
dayjs.locale(enGB);
var dateGB = dayjs(dateStringGB).format("LLLL");

console.log("dateUS: " + dateUS);
console.log("dateGB: " + dateGB);

and obtained

dateUS: Tuesday, September 10, 2019 8:51 AM
dateGB: Saturday, 9 November 2019 08:50

so the locales do work.

I was under the impression that parsing would also respect locales, but this doesn't seem to be the case?

@raketenolli
Copy link
Author

It seems CustomParseFormat supports adding a parsing format such as

var dateGB = dayjs(dateStringGB, "DD/MM/YYYY, HH:mm");

but no locale-supported format string such as

...dayjs(dateStringGB, "L, LT");

which would be useful here because the command could be used regardless of the locale currently set.

@raketenolli raketenolli changed the title Using locales without babel Locales can't be used in parsing dates Oct 16, 2019
@iamkun
Copy link
Owner

iamkun commented Oct 16, 2019

I did not get what you mean.

Using locale will localize the week name 'Saturday' to 'Saturday in another language', but will of course not display as another day like you expected 'Wed'

@raketenolli
Copy link
Author

Sorry, at first I was confused about using locales without Babel (require vs. import), but it turns out my problems were based on my expectations about using locales for parsing the dates. If I pass "11/09/2019" to dayjs for parsing with locale en-gb, I would expect 2019-09-11 as the date, not 2019-11-09 as it happened. Turns out I needed to set a CustomParseFormat, but this one only allows non-locale placeholders for the format, as above, e.g. "DD/MM/YYYY", whereas I had hoped for something like "L", or not format string necessary at all.

@iamkun
Copy link
Owner

iamkun commented Oct 17, 2019

@raketenolli CustomParseFormat plugin supports tokens like 'Do' 'MMMM' which are all locale-aware.

@raketenolli
Copy link
Author

I know that if you provide a string containing e.g. "März", it would be parsed properly to March if "MMMM" is provided as the format string for the CustomParseFormat and locale is set to "de". But the CustomParseFormat does not support the localized formats like "L", "LTS" etc. which would enable the user to just set the locale but not have to worry about the exact formatting if they know they can expect e.g. an "L, LTS" formatted string ("10/31/2019, 08:30 PM" vs. "31.10.2019, 20:30").

@iamkun
Copy link
Owner

iamkun commented Oct 17, 2019

Here's working around to get the locale formats
#680

@raketenolli
Copy link
Author

Thanks, so a solution to my issue would be

var L = dayjs.Ls[dayjs.locale()].formats.L;
var LT = dayjs.Ls[dayjs.locale()].formats.LT;
var dateParsedUsingCurrentLocale = dayjs(dateString, `${L}, ${LT}`);
console.log(dateParsedUsingCurrentLocale.format("LLLL"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants