7
14951

How to Convert Date to Unix Timestamp in JavaScript

Reading Time: 3 minutes

There are 4 ways to easily convert data to Unix Timestamp in JavaScript. Let’s explore the methods.

Methods to Convert Date to Unix Timestamp in JavaScript

Here are the list of 4 methods where we can easily convert date to Unix timestamp. Since each of them have their own properties and features, we can choose which to use depending on our needs.

  • Math.floor() Method
  • Custom Function to Convert Date to Unix TimeStamp
  • Custom Function to Convert Datetime to Unix TimeStamp
  • Moment.js Library

Let’s look into each details with illustration below.

1. Math.floor() Method

Math.floor() is part of original Math standard built-in object method in JavaScript. Combining with getTime() method of Date object, we will be able to retrieve Unix timestamp in seconds.

Process Flow:

  1. Create a Date object via the Date constructor taking in any acceptable date formats.
  2. We use getTime() method to retrieve the time in millisecond. Then we will divide the result by 1000 and use Math.floor() to remove all decimal values.
const date = new Date('2021-06-16');

const timeInMillisecond = date.getTime();

const unixTimestamp = Math.floor(date.getTime() / 1000);
console.log(unixTimestamp);   // 1623801600

2. Custom Function to Convert Date to Unix TimeStamp

We can create a common function to take in year, month and date that is reusable across whole codebase.

Process Flow:

  1. Create a function toUnixTime that takes in year, month and day.
  2. Inside toUnixTime function, we will create a Date object via the Date constructor taking in year, month and day. We will assume all pass-in parameter are UTC values by using Date.UTC.
  3. For month, we will subtract 1 from the input. As Date() take in month starting from 0 representing January.
  4. We use getTime() method to retrieve the time in millisecond. Then we will divide the result by 1000 and use Math.floor() to remove all decimal values.
const toUnixTime = (year, month, day) => {
  const date = new Date(Date.UTC(year, month - 1, day));
  return Math.floor(date.getTime()/1000);
}

const unixTimestamp = toUnixTime(2022, 12, 31);
console.log(unixTimestamp);   // 1672444800

3. Custom Function to Convert Datetime to Unix TimeStamp

To convert datetime to Unix Timestamp, it will be similar to the above method. However, we will also take in hour, minutes and seconds as parameters.

Process Flow:

  1. Create a function toUnixTime that takes in year, month and day, hour, min, sec.
  2. Inside toUnixTime function, we will create a Date object via the Date constructor taking in all 6 parameters. We will assume all pass-in parameter are UTC values by using Date.UTC.
  3. For month, we will subtract 1 from the input. As Date() take in month starting from 0 representing January.
  4. We use getTime() method to retrieve the time in millisecond. Then we will divide the result by 1000 and use Math.floor() to remove all decimal values.
const toUnixTime = (year, month, day, hr, min, sec) => {
  const date = new Date(Date.UTC(year, month - 1, day, hr, min, sec));
  return Math.floor(date.getTime()/1000);
}

const unixTimestamp = toUnixTime(2022, 12, 31, 12, 33, 0);
console.log(unixTimestamp);   // 1672489980

4. Moment.js Library

If we can use external library, Moment.js provides a very simple method for us to retrieve the Unix time from current or any date. unix() is the method we can use to retrieve dates in Unix timestamp in seconds.

Below shows the examples where we convert the current date, a specify date-string and unix milliseconds to unix seconds.

import moment from 'moment';

// Convert current time to Unix
let timestamp = moment().unix();
console.log(timestamp);          // 1651648727

// Convert datestring to unix seconds
timestamp = moment('2022-04-16').unix();
console.log(timestamp);          // 1650038400

// Convert a unix time in millisecond to second
timestamp = moment(1314535498806).unix();  
console.log(timestamp);          // 1314535498

Conclusion

We have look into 4 different methods to convert date to Unix Timestamp in JavaScript.

The easiest method is by using Moment.js library but we need to install a dependency. The next good option is to create a custom function since it will be reusable and also encapsulate the logic from external code.

Show Comments

No Responses Yet

Leave a Reply