From 820751cfcf236fc55c61398311479a29a08f85ce Mon Sep 17 00:00:00 2001 From: nonseodion Date: Thu, 8 Oct 2020 14:51:33 +0100 Subject: [PATCH 1/3] 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 From 0932d127751bbbf05a6d0d9a8bf6392624f3ffdc Mon Sep 17 00:00:00 2001 From: nonseodion Date: Thu, 8 Oct 2020 14:57:06 +0100 Subject: [PATCH 2/3] added Math.floor to timestamp --- snippets/timestamp.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/timestamp.md b/snippets/timestamp.md index 25aeb09ba..f739e9c5b 100644 --- a/snippets/timestamp.md +++ b/snippets/timestamp.md @@ -10,7 +10,7 @@ Gets the Unix timestamp from a `Date` object ```js const getTimestamp = date => -date.getTime()/1000 + Math.floor(date.getTime()/1000); ``` ```js From f117008eb6f78b07df96d7147fdc6d326d9237a1 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 8 Oct 2020 17:15:56 +0300 Subject: [PATCH 3/3] Update and rename timestamp.md to getTimestamp.md --- snippets/getTimestamp.md | 19 +++++++++++++++++++ snippets/timestamp.md | 18 ------------------ 2 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 snippets/getTimestamp.md delete mode 100644 snippets/timestamp.md diff --git a/snippets/getTimestamp.md b/snippets/getTimestamp.md new file mode 100644 index 000000000..54fac6213 --- /dev/null +++ b/snippets/getTimestamp.md @@ -0,0 +1,19 @@ +--- +title: getTimestamp +tags: date,beginner +--- + +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. + +```js +const getTimestamp = (date = new Date()) => + Math.floor(date.getTime() / 1000); +``` + +```js +getTimestamp(); // 1602162242 +``` diff --git a/snippets/timestamp.md b/snippets/timestamp.md deleted file mode 100644 index f739e9c5b..000000000 --- a/snippets/timestamp.md +++ /dev/null @@ -1,18 +0,0 @@ ---- -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 => - Math.floor(date.getTime()/1000); -``` - -```js -getTimestamp(new Date()); // 1602162242 -``` \ No newline at end of file