Files
30-seconds-of-code/snippets/getTimestamp.md
Isabelle Viktoria Maciohsek 920a0c390b Update snippet descriptions
2020-10-19 22:49:51 +03:00

19 lines
488 B
Markdown

---
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
```