To deal with date and time in javaScript there is a special built-in object - the Date
. It contains the date and time and provides a range of methods to manage both. One of the most frequently used methods of the Date object is getTime(). It returns the amount of milliseconds passed since midnight of January the 1st, 1970 (this date is also known as Unix Epoch).
Notes on the getTime() method
In order to call this method, you will need to create an instance of the Date
object first. No arguments are needed and the returned result would have a numeric data type. See the example.
const date = new Date();
console.log(date.getTime()); // logs a number above trillion
After running this code you will see a very long numeric value bigger than a trillion in the output. Moreover, it will be different every time you run the program. That’s because this number represents the amount of milliseconds passed from the Unix Epoch and the current date and time.
You can as well find the difference between the Epoch time and any other day. In order to do that you just need to specify the desired date in the constructor when creating an object of Date
. Moreover, the getTime()
method allows you to work with the events that happened before the Unix Epoch. In this case, the returned result will be a negative number.
For example, let’s find out how much time passed from the first landing on the Moon and the 1st of January, 1970. Note that the returned value would be negative and remain the same every time you run the program.
const moon_date = new Date("July 16, 1969");
console.log(moon_date.getTime()); // logs "-14612400000"
If you use an inappropriate date format while creating the Date object, this method will return NaN
as a result. For example, the NaN
value will be returned if you try to create an object for “January 32, 1970” since there are only 31 days in January.
The getTime()
always uses UTC for representing the time. Thus, the result of this method executed in a web browser in one time zone should be the same as in any other time zone.
Sometimes you may face situations when the precision of time returned by this method is reduced. It depends on the browser settings. For example, in Mozilla Firefox there is a special parameter - privacy.reduceTimerPrecision
which is usually enabled. It makes the getTime()
method round the amount of milliseconds and in this way helps to prevent various timer attacks or fingerprinting.
Examples
This method is functionally equivalent to the valueOf()
method applied to an instance of the Date
. Thus, you can not only use it to get and print the date but also to assign the returned value to another object.
For example, below is an instance of Date
for the 1st of February, 1970 created. Using the getTime()
method you can get this date converted into an amount of milliseconds and assign this value to a new object using the setTime()
method. In the output you will see the same date copied to a new object without any mistakes.
const date = new Date("February 1, 1970 01:00:00");
const copy_date = new Date();
copy_date.setTime(date.getTime());
console.log(copy_date); // logs Sun Feb 01 1970 01:00:00
This method is very convenient when you want to count the time passed between the two events or the time of executing some commands. For instance, let’s create a program to count how long the third power of 10 000 000 numbers will be calculated. For that purpose you will need to create an instance of Date
to store the time of beginning the calculation and another instance for the time of finish.
Applying the getTime()
method to both dates will give you two values. You can find out the duration of the calculating process by finding the difference between them. This implementation is illustrated below.
const start = new Date();
for (let i = 0; i< 10000000; i++) {
Math.pow(i, 3);
}
const finish = new Date();
console.log(finish.getTime() - start.getTime()); // logs 44
Note that for this purpose you can as well use the Date.now()
method allowing you to get a number of milliseconds passed since the Epoch time without creating any new objects and thus, works faster.
Of course, you can go further processing the returned amount of milliseconds in order to find seconds (divide the value by 1000), minutes (divide by 60*1 000), hours (divide by 60*60*1000) and so on.
Running the code below allows you to find the approximate amount of years passed since the Unix Epoch. The toFixed()
method is used to round the final amount and get rid of digits after the point.
const date = new Date();
const hours = date.getTime() / 3600000;
const days = hours / 24;
const years = days / 365;
console.log(years.toFixed()); // 52