From 820751cfcf236fc55c61398311479a29a08f85ce Mon Sep 17 00:00:00 2001 From: nonseodion Date: Thu, 8 Oct 2020 14:51:33 +0100 Subject: [PATCH] add timestamp --- snippets/timestamp.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/timestamp.md diff --git a/snippets/timestamp.md b/snippets/timestamp.md new file mode 100644 index 000000000..25aeb09ba --- /dev/null +++ b/snippets/timestamp.md @@ -0,0 +1,18 @@ +--- +title: functionName +tags: 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. + +```js +const getTimestamp = date => +date.getTime()/1000 +``` + +```js +getTimestamp(new Date()); // 1602162242 +``` \ No newline at end of file