Get the current date and time in London using JavaScript

avatar

Borislav Hadzhiev

Sun Jan 23 20223 min read

Get the current date and time in London using JavaScript #

Use the toLocaleString() method to get the current date and time in London, e.g. date.toLocaleString('en-GB', {timeZone: 'Europe/London'}). The toLocaleString method returns a string that represents the date according to the provided time zone.

index.js
const date = new Date(); // 👇️ 23/01/2022, 10:33:23 console.log( date.toLocaleString('en-GB', { timeZone: 'Europe/London', }), );

The toLocaleString method returns a locale-specific string that is adjusted to the provided time zone.

The two parameters we passed to the toLocaleString method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. en-US for US, es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options object where we specified the timeZone property. Read more about the options object in the MDN docs.

You can find a table of the country codes and time zone database names by visiting this wikipedia page.

Note that the Date object in JavaScript does not store a time zone, it stores a timestamp that represents the number of milliseconds that have passed since midnight on January 1st, 1970.

The date and time of the Date object correspond to the time zone, however the Date object has no way of storing the specific time zone.

For this reason, it's best to use the toLocaleString method to get a string that represents the time zone and use the options object parameter to format the string to your needs.

You can use the different properties on the options object of the toLocaleString method to format the date and time for the specific time zone in different ways.

index.js
const date = new Date(); // 👇️ Sunday, 23 January 2022 at 10:36:30 Greenwich Mean Time console.log( date.toLocaleString('en-GB', { timeZone: 'Europe/London', dateStyle: 'full', timeStyle: 'full', }), );

We set the dateStyle and timeStyle properties in the options object to full to get a more verbose representation of the date and time.

Other possible values for the two properties are: long, medium and short.

index.js
const date = new Date(); // 👇️ 23/01/2022, 10:38 console.log( date.toLocaleString('en-GB', { timeZone: 'Europe/London', dateStyle: 'short', timeStyle: 'short', }), );

You can view all of the properties and values the options object implements by visiting the MDN docs.

Here is an example that shows the month, day, hours, minutes and seconds as 2-digits, even if their values are less than 10.

index.js
const date = new Date(); // 👇️ 23/01/2022, 10:38:55 GMT console.log( date.toLocaleString('en-GB', { timeZone: 'Europe/London', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short', }), );

By setting the values of the date and time components to 2 digits, we format them consistently, even if they have a value of less than 10.

If that's the case, the values get padded with a leading zero.

We also set the timeZoneName property to a value of short, to show an abbreviation of the time zone name (GMT) at the end of the result.

You can view all the other properties the options object supports by visiting the MDN docs.

Use the search field on my Home Page to filter through my more than 1,000 articles.