Convert Date To Unix Timestamp In JavaScript

To convert date to Unix timestamp in JavaScript –

  1. Create a Date object using the Date constructor.
  2. Get the timestamp using the getTime() method.
  3. Divide the number by 1000 to get the timestamp in seconds.
  4. Use Math.floor() to get rid of the decimal places.
convert date to unix timestamp in JavaScript

Here is an example:

var d = new Date();

var ts = d.getTime() / 1000;

console.log(Math.floor(ts)); // 1511651700

You can pass a date string to the Date constructor.

If you do that, make sure to use the correct date format according to your locale.

For example, in the US it is common to use mm/dd/yyyy, whereas in most European countries it is dd/mm/yyyy.

Here is an example:

var d = new Date("12/25/1995");

var ts = d.getTime() / 1000;

console.log(Math.floor(ts)); // 811652800

Let’s see what each of the statement here does.

The first statement creates a Date object. To create a date object, you can pass the year, month and day as parameters to the Date constructor. If you don’t pass any parameters, by default it will create a date object for the current date and time.

Example

var date = new Date('2022-05-31');

console.log(date);

//output

//2022-05-31T00:00:00.000Z

There are other ways to create the date object.

var d = new Date(143214234); // timestamp in milliseconds

var d = new Date(2011, 2, 12); // year, month (0-11), day

var d = new Date(2022/05/31 12:23:23); // ISO 8601 format

To get the timestamp in seconds, we need to divide it by 1000 as there are 1000 milliseconds in a second.

The last statement uses the Math.floor() method to get rid of the decimal places. This is an optional step and you can also use the Math.round() method if you want to round off the value to the nearest integer.

Using Moment.js To Convert Date To Unix Timestamp In JavaScript

You can also use moment.js to convert dates.

Moment.js is a popular JavaScript library that makes working with dates and times easy.

To install Moment.js, use a package manager such as npm or yarn:

npm install moment
yarn add moment

Once you have installed Moment.js, you can use it to convert dates as follows:

var d = new Date();

var ts = moment(d).unix(); // 1511651700

console.log(ts);

The unix() method returns the timestamp in seconds. If you want the timestamp in milliseconds, you can use the valueOf() method:

var d = new Date();

var ts = moment(d).valueOf(); // 1511651700

console.log(ts);

You can also convert a date string to unix timestamp using Moment.js as follows:

var d = "12/25/1995";

var ts = moment(d, "MM/DD/YYYY").unix(); // 811652800

console.log(ts);

You can use the .parseZone() method to parse a zone as well:

var d = moment("12/25/1995", "MM/DD/YYYY").parseZone();

var ts = moment(d).unix(); // 811652800

console.log(ts);

Conclusion – Convert Date To Unix Timestamp In JavaScript

In this article, you have learned how to convert dates to unix timestamps using JavaScript. You have also learned how to use Moment.js to convert dates to timestamps.

I hope this article has been helpful and that you now know how to convert dates to unix timestamps using JavaScript. Thank you for reading!

Leave a Reply