Nest all content into snippets
This commit is contained in:
22
snippets/js/s/get-timestamp.md
Normal file
22
snippets/js/s/get-timestamp.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
title: Unix timestamp from date
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [date]
|
||||
cover: interior-14
|
||||
dateModified: 2020-10-19T22:49:51+03:00
|
||||
---
|
||||
|
||||
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
|
||||
```
|
||||
Reference in New Issue
Block a user