add daysFromNow.md

This commit is contained in:
nonseodion
2020-10-09 00:49:17 +01:00
parent c832e8b453
commit 2431ddc820

20
snippets/daysFromNow.md Normal file
View File

@ -0,0 +1,20 @@
---
title: daysFromNow.md
tags: date,beginner
---
Returns the date of `n` days from today as a string representation.
- Use `new Date()` and `Date.prototype.getDate()` to get the current time stamp add the number of milliseconds and use it to create a new `Date` object.
- Use `Date.prototype.toISOString()` to return a string in `yyyy-mm-dd` format.
```js
const daysFromNow = n =>{
let date = new Date(new Date().getTime() + n * 24 * 60 * 60 * 1000);
return date.toISOString().split('T')[0];
}
```
```js
daysFromNow('5'); // 2020-10-13
```