Files
30-seconds-of-code/snippets/js/s/unix-timestamp-from-date.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

587 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Unix timestamp from date snippet javascript
date
interior-14 2020-10-19T22:49:51+03:00

Gets the Unix timestamp from a Date object.

  • Use Date.prototype.getTime() to get the timestamp in milliseconds and divide by 1000 to 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