JavaScript

How to Convert Dates to Numbers in JavaScript

In the life of a JavaScript developer, formatting date or time is a common task. There exist various reasons to show or convert the current, past, and future times. You can convert dates to numbers if you want to calculate the time between two dates, calculate the age of someone, or find the execution time of a program.

In JavaScript, you can convert dates to numbers with the help of the “.getTime()” function of the “Date” Object. Don’t know how to use the “Date.getTime()” method? No worries! This article will assist you in this regard. So, let’s start!

What is Date object in JavaScript

In JavaScript language, the “Date” object is a built-in data type utilized to work with dates and timings. It also offers various methods for storing date in a variable, getting current time and date, formatting the date according to the user’s locale, and performing different arithmetic operations. By utilizing the “new” keyword, you can create a Date object in four different forms. To know more about it, check out the below-given syntax of the Date objects.

Syntax of Date object

Check out the following syntax for creating a new Date object with the current time and date:

new Date()

You can also create a new Date object by passing the milliseconds as arguments:

new Date(milliseconds)

To specify the year, month, day, hour, minutes, second, and millisecond, for a Date Object, use the below-given syntax:

new Date(year, month, date, hour, minute, second, millisecond)

You can also pass “dataString” as a parameter for the representation of the Date Object:

new Date(dataString)

How to access components of Date object in JavaScript

Several methods exists for accessing the components of Date object in JavaScript such as getMonth(), getHours(), getFullYear(). More specifically, we will talk about the “getTime()” method of the date object.

Date object getTime() method in JavaScript

The getTime() method in JavaScript outputs a numeric value related to the specified date in universal time. It returns the number of milliseconds since Epoch or January 1, 1970 (00:00:00).

The getTime() function of the Date object is also used for assigning time and date to another Date object. It also works in the Universal Time Zone, so if the getTime() function is called from multiple timezones, it will show the same results.

Syntax of getTime()

Here is the syntax of getTime() method:

Date.getTime()

How to convert Dates to Numbers in JavaScript

Using the Date.getTime() method, you can easily convert the dates into numbers. Want to try it out? If yes, then firstly define a Date Object in your JavaScript code.

const date1 = new Date();

console.log(date1);

In the above-given code, the “new Date()” constructor is utilized for creating a new Date “date1” object, and then it will output the current time and date with the help of the “console.log()” function:

Next, to convert the date into number, we will use the “getTime()” method in this way:

const result = date1.getTime();

console.log(result);

The execution of the above-given code will return the number of milliseconds between January 1, 1970 (EcmaScript epoch) and the current date:

You can also specify a “dataString” for converting the added date to numbers. For instance, in the below-given example, we will convert the “January 25, 1998 12:35:32” to numbers:

var x = new Date('January 25, 1998 12:35:32');

var y = x.getTime();

console.log(y);

When you execute the provided code, it will show the converted number as output:

Conclusion

This write-up demonstrated how to convert dates to numbers in JavaScript. Using the Date object getTime() method, you can easily convert the dates into numbers to calculate the time between two dates, calculate the age of someone, or find the execution time of a program. We have discussed the Date.getTime() method, its syntax, usage, and procedure of using it to convert the dates to numbers, in this writeup.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.