Nest all content into snippets
This commit is contained in:
25
snippets/js/s/last-date-of-month.md
Normal file
25
snippets/js/s/last-date-of-month.md
Normal file
@ -0,0 +1,25 @@
|
||||
---
|
||||
title: Last date of month
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [date]
|
||||
cover: polar-bear
|
||||
dateModified: 2020-10-09T22:01:42+03:00
|
||||
---
|
||||
|
||||
Returns the string representation of the last date in the given date's month.
|
||||
|
||||
- Use `Date.prototype.getFullYear()`, `Date.prototype.getMonth()` to get the current year and month from the given date.
|
||||
- Use the `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
|
||||
const lastDateOfMonth = (date = new Date()) => {
|
||||
let d = new Date(date.getFullYear(), date.getMonth() + 1, 0);
|
||||
return d.toISOString().split('T')[0];
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
lastDateOfMonth(new Date('2015-08-11')); // '2015-08-30'
|
||||
```
|
||||
Reference in New Issue
Block a user