In JavaScript, there are several ways to convert a date to a Unix timestamp, which represents the number of milliseconds since January 1, 1970, 00:00:00 UTC. Let's take a look at these methods one by one.

convert-date-to-unix-javascript

Using Date.prototype.getTime()

This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC, equivalent to the Unix timestamp. Here's an example:

const date = new Date('2023-04-01T08:30:00Z');
const unixTimestamp = date.getTime();
console.log(unixTimestamp); // output: 1672557000000

Using Date.parse()

Date.parse() is a built-in method in JavaScript that converts a date string into a Unix timestamp. In this method parses a string representation of a date and returns the Unix timestamp. Here's an example:

const dateString = '2023-04-01T08:30:00Z';
const unixTimestamp = Date.parse(dateString);
console.log(unixTimestamp); // output: 1672557000000

In this example, we have a date string in ISO 8601 format ('2023-04-01T08:30:00Z') representing April 1, 2023, at 8:30 AM UTC. We pass this string to Date.parse() which returns a Unix timestamp (1672557000000) that represents this date and time.

You can also pass other date formats to Date.parse(), such as RFC 2822 format or the date string format used by the toLocaleString() method. Here are some examples:

const dateString1 = 'Fri, 01 Apr 2023 08:30:00 GMT'; // RFC 2822 format
const timestamp1 = Date.parse(dateString1);
console.log(timestamp1); // output: 1672557000000

const dateString2 = '4/1/2023, 8:30:00 AM'; // date string format
const timestamp2 = Date.parse(dateString2);
console.log(timestamp2); // output: 1672557000000

In both examples, we pass a different date string format to Date.parse() but the result is the same Unix timestamp.

Using Math.Floor

Math.floor() is a built-in function in JavaScript that rounds down a given number to the nearest integer.

This method rounds down the Unix timestamp to the nearest integer.

const date = new Date('2023-04-01T08:30:00Z');
const unixTimestamp = Math.floor(date.getTime() / 1000);
console.log(unixTimestamp); // output: 1672557000

In the above code, we divide the result of getTime() by 1000 because the Unix timestamp is represented in seconds, not milliseconds.

Using Moment.js

This method requires you to use an external library, Moment.js, but it can be useful if you are already using this library in your project.

Moment.js library provides various methods to work with dates and times in JavaScript, including converting dates to Unix timestamps.

const moment = require('moment');
const date = new Date('2023-04-01T08:30:00Z');
const unixTimestamp = moment(date).unix();
console.log(unixTimestamp); // output: 1672557000

In the above example, we first install the moment.js library using the require() function, then we pass the date object to the moment() function and call the unix() method to get the Unix timestamp.

You may also like to read:

Read Excel file using Javascript (XLSX or XLS)

Read CSV file in Javascript and HTML5 FileReader (Multiple ways)

Best Javascript Drag and Drop Libraries

Create a simple calculator using Javascript

how to check if string is null or empty using jQuery or javascript?

Various ways to make HTTP Request in Javascript