Learning JavaScript — Part 17

Jeff P
2 min readOct 13, 2022

--

Times and Dates

new Date() will show the current time.

console.log(new Date());

returns…

Thu Oct 13 2022 14:28:26 GMT+0100 (British Summer Time)

We can get the start of unix time, by using 0

console.log(new Date(0));

returns

Thu Jan 01 1970 01:00:00 GMT+0100 (Greenwich Mean Time)

There are also options we can call on dates….

const now = new Date();console.log(now.getSeconds()); 
console.log(now.getMinutes());
console.log(now.getHours());
console.log(now.getDay()); // day of week - Sunday is 0
console.log(now.getDate()); // day of the month
console.log(now.getMonth()); // January is 0
console.log(now.getFullYear());
console.log(now.getTime()); // unix time
console.log(now.toISOString());

returns the following:

35
58
14
4
13
9
2022
1665669515686
2022-10-13T13:58:35.686Z

Date Internationalization

currencies and dates are often represented differently in different countries. For example, in the US, they express the date as month/day/year (10/13/22) whereas in the UK we show it as day/month/year (13/10/22)

Also for currencies, Germany will show 50 Euros as 50 € whereas in the UK we would show this as €50

For dates we use .DateTimeFormat()

DateTimeFormat()

Used with new Intl. For example…

const currentDate = new Date();
const ukDate = new Intl.DateTimeFormat("en-GB").format(currentDate);
const usDate = new Intl.DateTimeFormat("en-US").format(currentDate);
console.log(ukDate);
console.log(usDate);

which returns

13/10/2022
10/13/2022

you can get the codes from around the world from http://www.lingoes.net/en/translator/langcode.htm

You can also supply various options as a second argument, although for ease of reading, these options are usually stored in an object, and then referenced. For example:

const currentDate = new Date();
const options = {
minute: "numeric",
hour: "numeric",
day: "numeric",
month: "long",
year: "numeric",
weekday: "long",
};
const ukDate = new Intl.DateTimeFormat("en-GB", options).format(currentDate);
const usDate = new Intl.DateTimeFormat("en-US", options).format(currentDate);
console.log(ukDate);
console.log(usDate);

which returns

Thursday, 13 October 2022 at 15:28
Thursday, October 13, 2022 at 3:28 PM

As you can see, the US way of showing the time is with AM or PM, whereas the UK standard is to show the time as 24-hour.

You can also use the locale of the users browser, which will be different for each person…. for example

const currentDate = new Date();
const locale = navigator.language;
const options = {
minute: "numeric",
hour: "numeric",
day: "numeric",
month: "long",
year: "numeric",
weekday: "long",
};
const ukDate = new Intl.DateTimeFormat(locale, options).format(currentDate);

console.log(ukDate);

numbers

We can also format numbers…

const currency = 1277328.23;
const options = {
style: "currency",
currency: "EUR",
};
const ukCurrency = new Intl.NumberFormat("en-GB", options).format(currency);
const germanyCurrency = new Intl.NumberFormat("de-DE", options).format(currency);
console.log(ukCurrency);
console.log(germanyCurrency);

returns

€1,277,328.23
1.277.328,23 €

--

--

Jeff P
Jeff P

Written by Jeff P

I tend to write about anything I find interesting. There’s not much more to it than that really :-)

No responses yet