Skip to content

Commit 820751c

Browse files
committed
add timestamp
1 parent cd93e7e commit 820751c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

snippets/timestamp.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: functionName
3+
tags: date,beginner
4+
---
5+
6+
Gets the Unix timestamp from a `Date` object
7+
8+
- 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.
9+
- Use `Math.floor()` to appropriately round the resulting timestamp to an integer.
10+
11+
```js
12+
const getTimestamp = date =>
13+
date.getTime()/1000
14+
```
15+
16+
```js
17+
getTimestamp(new Date()); // 1602162242
18+
```

0 commit comments

Comments
 (0)