To convert a date to a Unix timestamp in JavaScript, you can use the getTime()
method of the Date
object. Here's an example:
// Create a new Date object with the desired date
let date = new Date('2022-01-01');
// Use getTime() method to get the Unix timestamp
let unixTimestamp = date.getTime() / 1000;
console.log(unixTimestamp); // Output: 1640995200
In the example above, a new Date
object is created with a desired date '2022-01-01'
. The getTime()
method is then used to get the number of milliseconds since January 1, 1970. To convert it to a Unix timestamp, divide it by 1000. Finally, the result is logged using console.log()
.