Add fromTimestamp
This commit is contained in:
17
snippets/fromTimestamp.md
Normal file
17
snippets/fromTimestamp.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
title: fromTimestamp
|
||||
tags: date,beginner
|
||||
---
|
||||
|
||||
Creates a `Date` object from a Unix timestamp.
|
||||
|
||||
- Convert the timestamp to milliseconds by multiplying with `1000`.
|
||||
- Use `new Date()` to create a new `Date` object.
|
||||
|
||||
```js
|
||||
const fromTimestamp = timestamp => new Date(timestamp * 1000);
|
||||
```
|
||||
|
||||
```js
|
||||
fromTimestamp(1602162242); // 2020-10-08T13:04:02.000Z
|
||||
```
|
||||
Reference in New Issue
Block a user