Update lastDateOfMonth.md
This commit is contained in:
committed by
GitHub
parent
d411e68669
commit
b19b1174e7
@ -3,16 +3,19 @@ title: lastDateOfMonth
|
|||||||
tags: date,intermediate
|
tags: date,intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
Get last date in current month of given date.
|
Returns the string representation of the last date in the given date's month.
|
||||||
- Generate new date of the next month and use day 0 to get one day before it.
|
|
||||||
|
- Use `Date.prototype.getFullYear()`, `Date.prototype.getMonth()` to get the current year and month from the given date.
|
||||||
|
- Use the `new Date()` constructor to create a new date with the given year and month incremented by `1`, and the day set to `0` (last day of previous month).
|
||||||
|
- Omit the argument, `date`, to use the current date by default.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const lastDateOfMonth = date => {
|
const lastDateOfMonth = (date = new Date()) => {
|
||||||
return new Date(date.getFullYear(),date.getMonth()+1,0);
|
let d = new Date(date.getFullYear(), date.getMonth() + 1, 0);
|
||||||
}
|
return d.toISOString().split('T')[0];
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
//Current date is 10-10-2020
|
lastDateOfMonth(new Date('2015-08-11')); // '2015-08-30'
|
||||||
lastDateOfMonth(new Date()); // 'Sat Oct 31 2020 00:00:00 GMT+0700'
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user