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.

Format dates acccording to the current locale

See original GitHub issue

In many localization scenarios, locales can have different formats for dates. Think en vs jp, for example. Or en-US vs en-GB.

It is thus required to define different date formats for each locale, preferably even in the locale json file itself, so a person doing localization would be able to tune the formats if needed.

It’s very hard to figure out how to change date formats at all, here is a working example:

  i18n.init({
    ...
    formats: {
      date: {
        short: {month: 'numeric', day: 'numeric', year: 'numeric'}, // short seems to be the default
        full: {year: 'numeric', month: 'short', day: 'numeric'}
      }
    }
  })

However, it seems to be using exactly the same format for each different locale, maybe according to the browser’s default locale?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kaisermanncommented, Feb 28, 2020

@rodoch Got pretty much everything right! Thanks for the great explanation 😁 It’s not hard to create a derived store from $locale to format dates with another lib:

const myDateFormatter = derived([locale], (locale) => (date) => dateLib(date, locale))
1reaction
rodochcommented, Feb 28, 2020

I’m going to presume this isn’t the cause of your issue, but just in case: I believe svelte-i18n, at some level, wraps the Intl API. The Intl API only has access to whatever locales are available in your runtime (server-side: Node.js, client-side: whatever the browser ships with). This bit me recently working with ga-IE localisation in Ireland: Firefox and Edge ship with better localised distros here and svelte-i18n was localising the dates for me, but whenever I opened the app in Chrome the dates were displayed in English as Chrome had no support for the ga-IE locale. In the end the only solution that reliably worked in userland was to use moment.js for date formatting (manually importing ga-IE locale) and svelte-i18n for the rest of the string translation. It works good though, and the way svelte-i18n v3 has broken up the _, number and date functions so they can be tree-shaken reduces extra weight involved with adding an additional library.

Hope I haven’t got any of the above wrong @kaisermann

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date Formats (International Language Environments Guide)
Locale Convention Example Canadian (English and French) yyyy‑mm‑dd 1998‑08‑13 Danish yyyy‑mm‑dd 1999‑08‑24 Finnish dd.mm.yyyy 13.08.1998
Read more >
How can I format date by locale in Java? - Stack Overflow
Yes, using DateFormat.getDateInstance(int style, Locale aLocale) This displays the current date in a locale-specific way. So, for example:
Read more >
Date and time localization - Lokalise Blog
Here, the month and date can be provided in any order. For example, both Jan 21 2020 and 21 Jan 2020 are valid...
Read more >
Location based Date Time Formatting - HowToDoInJava
Learn to display the date and time information in Java to the end-user in a locale-sensitive manner, based on the location and timezone....
Read more >
Date.prototype.toLocaleDateString() - JavaScript | MDN
A string representing the date portion of the given Date instance according to language-specific conventions. In implementations with Intl.
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