Text Center
Text left
Text Right
This article is a transcript of my free YouTube series about the basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.
Hi there my fellow newbs! Another episode of the Date methods is here. Today, we are going to have a look at the STATIC methods of the Date object. There’s three of them and I will explain and showcase each of them. Let’s begin.
The first static method is called now() and it returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. The return value is a primitive integer value.
This method was specified in 5th edition of the EcmaScript specification, so it is still possible that some ancient versions of browsers do not support it. Although I believe you will not encounter such a case. But if you somehow do, you can simply override the issue by using the non-static method of getTime().
Let’s see some examples of this static method now.
console.log(Date.now()); console.log(Date.now()); console.log(Date.now()); console.log(Date.now()); console.log(Date.now());// 1626156551132 // 1626156551133 // 1626156551133 // 1626156551133 // 1626156551133
Some browsers, for example Firefox, provide reduced time precision when using Date.now() method. What it means is that it offers protection against various timing attacks and fingerprinting by rounding the exact value to a certain precision.
I checked in different browsers and Chrome does not provide such a behaviour. You can see that you can get the same milliseconds even multiple times — I presume depending on the current load of the processor.
Second static method is called parse() and as it stands from its name, its job is that it parses a string representation of a date. If it is successful, it returns the number of milliseconds since 1st January 1970, 00:00:00 UTC. If the parsing fails because the string is not recognised or the date contains illegal date values, the “NaN” constant is returned instead.
This method is not historically very reliable and until EcmaScript version 5, the parsing implementation was completely dependent on a vendor. It still is not quite unified to this day, but at least most common formats are parseable in each browser implementation. Officially the simplified format of ISO 8601 should be supported by all the implementations, but fortunately the set of parsable formats is bigger than that. I will show all the cases I could come up with in the examples. As was mentioned before, the returned value is the number representing the milliseconds elapsed since the UNIX Epoch. Now, to those examples…
// uses 00:00:00.000 in local time
console.log(Date.parse("2021-07-07"));
// 1625616000000// uses 00:00:00.000 in UTC/GMT
console.log(Date.parse("07 Jul 2021"));
// 1625608800000// these 2 use local time
console.log(Date.parse("2021-07-07T22:19:38"));
// 1625689178000console.log(Date.parse("Wed, 07 Jul 2021 22:19:38"));
// 1625689178000// these 2 use explicitly specified UTC/GMT
// parse() method can only handle GMT, not UTC or any other time zone shortcut
console.log(Date.parse("07 Jul 2021 20:19:38 GMT"));
// 1625689178000console.log(Date.parse("Wed, 07 Jul 2021 20:19:38 GMT"));
// 1625689178000// specifying UTC/GMT time zone by +00:00
console.log(Date.parse("2021-07-07T20:19:38.054+00:00"));
// 1625689178054// specifying CET time zone by +02:00
console.log(Date.parse("2021-07-07T22:19:38.054+02:00"));
// 1625689178054// Z - represents GMT/UTC in ISO 8601 format
console.log(Date.parse("2021-07-07T20:19:38.054Z"));
// 1625689178054
I divided all the formats that can be parsed into three groups. First one specifies only year, month and day, the rest is utilizing the default values. Interesting is that while the first format uses local time, the second one uses GMT/UTC.
In the second group are formats that also specify hours, minutes and seconds, but do not mention milliseconds. Situation is as follows. If the GMT time zone is not specified explicitly, the local time zone is used instead. It is also interesting to observe the strange behaviour of parses. It processes shortcut GMT, but it can not process UTC or any other time zone shortcut like CET.
The last group also specifies milliseconds. The UTC or rather GMT time zone can be specified in at least two different ways. We can either use standard and universal “+00:00” or we can use the shortcut “Z”. And that’s that about the static method parse().
The last method is the UTC() method which accepts parameters similar to the Date constructor, but treats them as UTC. It returns the number of milliseconds since UNIX Epoch.
The UTC() method behaves exactly like the Date constructor even when processing year values from range between 0 and 99. Hint: it processes them as if you typed in values from range 1900 to 1999.
Let’s see some examples and I will tell you something more about this method afterwards.
// this date is specified in local time (GMT+0100)
let endDate = new Date(2021,11,31,23,59,59,999);
console.log(endDate.getTime());
// 1640991599999console.log(endDate);
// Fri Dec 31 2021 23:59:59 GMT+0100 (stredoeurópsky štandardný čas)// this date is specified in UTC (GMT+0000)
let utcDate = Date.UTC(2021,11,31,23,59,59,999);
console.log(utcDate);
// 1640995199999console.log(new Date(utcDate));
// Sat Jan 01 2022 00:59:59 GMT+0100 (stredoeurópsky štandardný čas)// this date is specified in UTC (GMT+0000)
// 23 months - 12 months = 11 months
// year 2020 + 12 months = year 2021
let wrongUtcDate = Date.UTC(2020,23,31,23,59,59,999);
console.log(wrongUtcDate);
// 1640995199999console.log(new Date(wrongUtcDate));
// Sat Jan 01 2022 00:59:59 GMT+0100 (stredoeurópsky štandardný čas)
This method actually differs from the Date constructor only in two things. First is that it uses universal time instead of local time. And the second is that it does return the number as a time value instead of the Date object.
If any parameter gets out of range for some reason, the method accommodates the value. So for example if you type in 23 as month, it will increase year by one and then subtract 12 months from the value 23, arriving at the value of 11 which is then used as a value for month position. Pretty cool, if you ask me.
But that’s all I got for you today. So I am afraid that we will have to call it a day. Hope you liked it.
As always, thanks for your undying attention and I will see you next time with the methods that actually format the Date object as we need.