Convert date to milliseconds since epoch in JavaScript
This post will discuss how to convert a date to milliseconds since the epoch in JavaScript.
There are several ways to convert a date to milliseconds since the epoch in JavaScript. A date is a representation of a specific point in time, but the epoch is a fixed point in time, which is January 1, 1970, 00:00:00 UTC. Milliseconds since epoch is the number of milliseconds that have elapsed since the epoch. Here are some of the functions we can use:
1. Using Date.prototype.getTime() function
To convert a date to milliseconds since the epoch in JavaScript, we need to get the numeric value of the date object that represents the date. There are different ways to get the numeric value of a date object, but one common function is to use the getTime() function. The following function creates a Date object from the input date and uses the getTime() function to get the number of milliseconds since epoch:
|
1 2 3 4 5 6 7 8 |
// Create a `Date` object from the input date var date = new Date("2020-12-31"); // Get milliseconds since epoch using the getTime function var milliseconds = date.getTime(); // The milliseconds variable now contains the number of milliseconds since epoch for the input date console.log(milliseconds); // 1609372800000 |
Note that the Date object can parse various date formats, such as yyyy-mm-dd, mm/dd/yyyy, or dd/mm/yyyy. We can also use the valueOf() function which is another instance function of Date object. We can use it in the same way as getTime(), as shown below:
|
1 2 3 4 5 6 7 |
// Create a `Date` object from the input date var date = new Date("2020-12-31"); // Get milliseconds since epoch using the valueOf function var milliseconds = date.valueOf(); console.log(milliseconds); // 1609372800000 |
Another option is to use the unary plus operator (+) to convert a date object to a number, which will return the same value as the getTime() function. For instance:
|
1 2 3 4 5 6 7 |
// Create a `Date` object from the input date var date = new Date("2020-12-31"); // Get milliseconds since epoch using unary plus operator (+) var milliseconds = +date; console.log(milliseconds); // 1698134400000 |
2. Using Date.parse() function
Another way to convert a date to milliseconds since the epoch in JavaScript is using the Date.parse() function. This is a built-in function that parses a date string and returns the number of milliseconds elapsed since the epoch. The string can be in various formats, such as ISO 8601 (yyyy-mm-ddThh:mm:ss.sssZ), RFC 2822 (ddd, dd mmm yyyy hh:mm:ss GMT), or other common formats. For instance:
|
1 2 3 4 |
// Parse an ISO 8601 string and get the number of milliseconds since epoch var milliseconds = Date.parse("2020-12-31T23:59:59.999Z"); console.log(milliseconds); // 1609459199999 |
3. Using Moment.js library
This is a more convenient way to convert a date to milliseconds since the epoch in JavaScript. Moment.js is a popular library that provides various functions for manipulating and formatting dates. To convert a date to milliseconds since the epoch using Moment.js, we can use its valueOf() function. This function returns the number of milliseconds since epoch for the current moment object. We can create a moment object from various inputs, such as strings, numbers, arrays, or native Date objects. To use Moment.js, we need to include it in our HTML file or import it in our JavaScript file. For instance:
|
1 2 3 4 5 6 7 8 9 10 |
// Import Moment.js in our JavaScript file const moment = require("moment"); // Create a moment object from a string in dd/mm/yyyy format var momentObj = moment("31/12/2020", "DD/MM/YYYY"); // Get milliseconds since epoch using the valueOf function var milliseconds = momentObj.valueOf(); console.log(milliseconds); // 1609372800000 |
That’s all about converting a date to milliseconds since the epoch in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)