Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
JavaScript Program to Convert Date to Number
In this example, you will learn to write a JavaScript program that will convert a date to a number.
To understand this example, you should have the knowledge of the following JavaScript programming topics:
Example: Convert Date to Number
// program to convert date to number
// create date
const d1 = new Date();
console.log(d1);
// converting to number
const result = d1.getTime();
console.log(result);
Output
Mon Nov 09 2020 10:52:32 GMT+0545 (Nepal Time) 1604898452084
In the above example, the new Date() constructor is used to create a date object.
The new Date() gives the current date and time.
const d1 = new Date();
console.log(d1); // Mon Nov 09 2020 10:52:32 GMT+0545 (Nepal Time)
To convert the name to a number, we use the getTime() method.
The getTime() method returns the number of milliseconds from midnight of January 1, 1970 (EcmaScript epoch) to the specified date.
const d1 = new Date().getTime();
console.log(d1); // 1604898452084
Also Read:
Did you find this article helpful?