Files
30-seconds-of-code/snippets/getTimestamp.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

564 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
getTimestamp date,beginner 2020-10-08T17:15:56+03:00 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