Files
30-seconds-of-code/snippets/timestamp.md
2020-10-08 14:57:06 +01:00

465 B

title, tags
title tags
functionName date,beginner

Gets the Unix timestamp from a Date object

  • Use new Date() to get the Date object and 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.
const getTimestamp = date =>
  Math.floor(date.getTime()/1000);
getTimestamp(new Date()); // 1602162242