International date sites

A Definitive Way to Format Dates for International Sites

Date formats vary with region and language so, it is always helpful if we can find a way to display the dates to the users, specific to their language and region.

Back in December, 2012, ECMA released the specifications of Internationalization API for JavaScript. The Internationalization API helps us display certain data according to the language and cutural specification. It can be used to identify currencies, time zones and more.

In this post we will be looking into date formatting using this API.

Know the user’s locale

To show the date as per user’s preferred locale, first we need to know what that preferred locale is. Currently the foolproof way to know that is to ask the user; let users select their preferred language and region settings in the webpage.

But, if that is not an option you can interpret the Accept-Language request header or read the navigator.language(for Chrome and Firefox) or navigator.browserLanguage(for IE) values.

Please know that not all of those options return the preferred language of the browser UI.

var language_tag = window.navigator.browserLanguage || window.navigator.language || "en"; // returns language tags like 'en-GB'

Check for Internationalization API

To know if the browser supports Internationalization API or not, we can check for the presence of the global object Intl.

if(window.hasOwnProperty​("Intl") && typeof Intl === "object"){ // Internationalization API is present, let us use that }

The Intl object

Intl is a global object for using the Internationalization API. It has three properties which are constructors for three objects namely Collator, NumberFormat, and DateTimeFormat.

The object we will be using is DateTimeFormat which will help us format date time as per different languages.

The DateTimeFormat object

The DateTimeFormat constructor takes two optional arguments;

  • locales – a string or an array of strings that represent the language tags, for example; “de” for German language, “en-GB” for English used in United Kingdom. If a language tag is not mentioned, the default locale will be that of runtime.
  • options – an object whose properties are used to customize the formatter. It has the following properties:
Property Description Possible values
day Day of the month “2-digit”, “numeric”
era Era the date falls into, Ex: BC “narrow”, “short”, “long”
formatMatcher The algorithm used for format matching “basic”, “best fit”[Default]
hour Represents Hours in the time
hour12 Indicates 12-hour format(true) or 24-hour format(false) true, false
localeMatcher The algorithm used for locale matching “lookup”, “best fit”[Default]
minute Minutes in the time
month Month in a year “2-digit”, “numeric”, “narrow”, “short”, “long”
second Seconds in the time
timeZone Time zone to apply “UTC”, default is runtime time zone
timeZoneName Time zone of the date “short”, “long”
weekday Day in the week
year Year of the date

Example:

var formatter = new Intl.DateTimeFormat('en-GB'); /* returns a formatter that can format a date in UK English date format */

var options = {weekday: 'short'}; var formatter = new Intl.DateTimeFormat('en-GB', options); /* returns a formatter that can format a date in UK English date format * along with the weekday in short notation like 'Thu' for Thursday */

The format function

The instance of the DateTimeFormat object has an property accessor (getter) called format which returns a function that formats a Date based on the locales and options found in the DateTimeFormat instance.

The function takes a Date object or undefined as an optional argument and returns a string in the requested date format.

Note: If the argument is either undefined or not provided then it returns the value of Date.now in the requested date format.

Here’s the syntax:

new Intl.DateTimeFormat.format //will return the current date in the runtime date format

And now let us code a simple date formatting.

Let us change the language and see the output.

Now, it is time to look into options.

The toLocaleDateString method

Instead of using a formatter like shown in the above examples, you can also use Date.prototype.toLocaleString in the same way with the locales and options arguments, they are similar but it is recommended to use the DateTimeFormat object when dealing with too many dates in your application.

var mydate = new Date('2015/04/22'); var options = { weekday: "short", year: "numeric", month: "long", day: "numeric" }; console.log(mydate.toLocaleDateString('en-GB', options)); // returns "Wed, 22 April 2015"

Test if the locales are supported

To check for the supported locales, we can use the method supportedLocalesOf of DateTimeFormat object. It returns an array of all supporting locales or an empty array if none of the locales is supported.

For testing, let us add a dummy locale “blah” in the list of locales to be checked.

console.log(Intl.DateTimeFormat.supportedLocalesOf(["zh", "blah", "fa-pes"])); // returns Array [ "zh", "fa-pes" ]

BookFactory 2016 Weekly Pocket Calendar / 2016 Calendar / 2016 Weekly Calendar / Weekly Planner Organizer - Calendar with Notepad (CAL-2016-POCKET(Organizer))
Office Product (BookFactory)
  • Black and tan cover with blind embossed 2016 date
  • Includes 16 page full color World Map. Weights and Measure conversion chart, Local and International calling codes
  • Time Zones (North America) Important toll Free numbers and web Sites. A First Aid spread with details for CPR and choking
  • An area for Birthdays, Anniversaries, Addresses and Telephone numbers and notepad
  • BookFactory is a Veteran-Owned Firm in Ohio.
You might also like:
NASA Announces Dates for International Space Apps Challenge
NASA Announces Dates for International Space Apps Challenge
Mike And Dave Need Wedding Dates (International Trailer 1)
Mike And Dave Need Wedding Dates (International Trailer 1)
Vintage 1864 Map of Preliminary map of northeastern Virginia, south of the Rappahannock, east of the Blue Ridge, and north of 38â° N. Lat. - Shows names of some residents. - Relief shown by hachures. - Date and title from Stephenson's Civil War maps, 1989. - Sun print on a number of sheets pieced together and sectioned in two, with extensive ink and pencil annotations including a colored ink United States, Virginia
Home (Historic Map)
  • Vintage 1864 Map of Preliminary map of northeastern Virginia, south of the Rappahannock, east of the Blue Ridge, and north of 38â° N. Lat. - Shows names of some...
  • Map size: 2 foot by 18 inches
  • Beautiful and Rare Historic Map
  • Archival Quality Reproduction
Due Date International Trailer
Due Date International Trailer
Best International Dating Site Review - Asian Date Review
Best International Dating Site Review - Asian Date Review
Liili Products Liili Premium Samsung Galaxy Note 2 Aluminum Case Colorful umbrellas at a shopping mall in Melbourne Australia Image ID 22531428
Wireless (Liili Products)
  • MADE IN USA. Designed, Printed and Shipped out of our California Facility.
  • Easy access to all buttons and controls
  • This item is designed and made for Samsung Galaxy Note 2 Only.
  • Please Search Title Image ID for additional products.
  • Looking for more design in the field? Please search Liili Samsung Galaxy Note 2 for more special and unique design.
Prints Online Photo Jigsaw Puzzle of Chicago World Fair - The Site looking South
Home (Prints Online)
  • PHOTO JIGSAW PUZZLE You are purchasing one Photo Puzzle (252 Pieces). Estimated image size 356x254mm
  • 10x14 252 Piece Puzzle comes with loose pieces in a designer box with your photo on top. The box is sized to fit most mail boxes. Great gift for kids, grandparents...
  • Artwork Description Chicago World Fair - The Site looking South Date 1933.
  • For any queries regarding this choice of artwork please contact Prints Online. Image (c) Mary Evans Picture Library 2015 -
  • To view this image on other products please search for code 7184343
Safety Technology Intl Safety Technology International STI-34400 Wireless Universal Alert - Part of the Wireless Alert Series
Home Improvement (Safety Technology Intl)
  • Will let you know if a monitored site is tripped
  • Has a supervised battery and signal reception
  • Operates up to 1 (line of sight)
  • Includes 3 volt lithium battery (estimated battery life is 5 years with normal use)
  • Can be used as an on board reed switch, set of terminals (remote) or as a tilt switch
Related Posts