question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Locales can't be used in parsing dates

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
raketenollicommented, Oct 17, 2019

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"));
0reactions
iamkuncommented, Oct 17, 2019

Here’s working around to get the locale formats https://github.com/iamkun/dayjs/issues/680

Read more comments on GitHub >

github_iconTop Results From Across the Web

apex - Date.parse not working for different locales
It works fine if the logged in user's locale is English(US) but is failing for other locales because of invalid date format. As...
Read more >
GWT parse date in different locales - java - Stack Overflow
I have date from server, represented by string like "Thu Oct 31 2013 00:00:00 GMT+0200" and i have to parse it in Date...
Read more >
Problem using Locale Date Format Conversions .. Need Help
I need to Internationalize my Application to Locale Settings for Dates. ... If the century cant be assumed , the 2 digit year...
Read more >
4 Ways to Fix Date Errors in Power Query + Locale & Regional ...
The first way to fix a date error that stems from location differences is to choose the Using Locale setting in the Data...
Read more >
Formatting / Parsing - Manual | js-joda
For example, the format eeee (d MMMM) might return Saturday (28 April) in English, and samedi (28 avril) in French. If you try...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found