Update and rename timestamp.md to getTimestamp.md
This commit is contained in:
committed by
GitHub
parent
0932d12775
commit
f117008eb6
19
snippets/getTimestamp.md
Normal file
19
snippets/getTimestamp.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -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
|
|
||||||
```
|
|
||||||
Reference in New Issue
Block a user