488 B
488 B
title, tags
| title | tags |
|---|---|
| getTimestamp | date,beginner |
Gets the Unix timestamp from a Date object.
- Use
Date.prototype.getTime()to get the timestamp in milliseconds and divide by1000to get the timestamp in seconds. - Use
Math.floor()to appropriately round the resulting timestamp to an integer. - Omit the argument,
date, to use the current date.
const getTimestamp = (date = new Date()) => Math.floor(date.getTime() / 1000);
getTimestamp(); // 1602162242