Files
30-seconds-of-code/javascript/snippets/days-ago.md
2023-05-01 22:35:56 +03:00

26 lines
673 B
Markdown

---
title: Days ago
type: snippet
tags: [date]
cover: sunrise-over-city
dateModified: 2022-01-30T11:48:07+03:00
---
Calculates the date of `n` days ago from today as a string representation.
- Use the `Date` constructor to get the current date.
- Use `Math.abs()` and `Date.prototype.getDate()` to update the date accordingly and set to the result using `Date.prototype.setDate()`.
- Use `Date.prototype.toISOString()` to return a string in `yyyy-mm-dd` format.
```js
const daysAgo = n => {
let d = new Date();
d.setDate(d.getDate() - Math.abs(n));
return d.toISOString().split('T')[0];
};
```
```js
daysAgo(20); // 2020-09-16 (if current date is 2020-10-06)
```