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
is part of original Math standard built-in object method in JavaScript. Combining with Math.floor()
getTime()
method of Date object, we will be able to retrieve Unix timestamp in seconds.
Process Flow:
- Create a
Date
object via theDate
constructor taking in any acceptable date formats. - We use
getTime()
method to retrieve the time in millisecond. Then we will divide the result by1000
and useMath.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:
- Create a function
toUnixTime
that takes inyear
,month
andday
. - Inside
toUnixTime
function, we will create aDate
object via theDate
constructor taking inyear
,month
andday
. We will assume all pass-in parameter are UTC values by usingDate.UTC
. - For
month
, we will subtract 1 from the input. AsDate()
take in month starting from 0 representing January. - We use
getTime()
method to retrieve the time in millisecond. Then we will divide the result by1000
and useMath.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:
- Create a function
toUnixTime
that takes inyear
,month
andday
,hour
,min
,sec
. - Inside
toUnixTime
function, we will create aDate
object via theDate
constructor taking in all 6 parameters. We will assume all pass-in parameter are UTC values by usingDate.UTC
. - For
month
, we will subtract 1 from the input. AsDate()
take in month starting from 0 representing January. - We use
getTime()
method to retrieve the time in millisecond. Then we will divide the result by1000
and useMath.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.
No Responses Yet