Skip to content

Commit

Permalink
Add lastDayOfMonth support
Browse files Browse the repository at this point in the history
  • Loading branch information
slimcandy committed Feb 8, 2023
1 parent 1bcf3c9 commit aed8596
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/code-sandbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"date-fns/08-month/getMonth": "https://codesandbox.io/s/jqquk?previewwindow=tests&file=/08-month/getMonth/vanilla.js",
"date-fns/08-month/isFirstDayOfMonth": "https://codesandbox.io/s/ltltp?previewwindow=tests&file=/08-month/isFirstDayOfMonth/vanilla.js",
"date-fns/08-month/isLastDayOfMonth": "https://codesandbox.io/s/d9veq?previewwindow=tests&file=/08-month/isLastDayOfMonth/vanilla.js",
"date-fns/08-month/lastDayOfMonth": "https://codesandbox.io/s/date-fns-lastdayofmonth-eun1ls?previewwindow=tests&file=/00-generic/compareDesc/vanilla.js",
"date-fns/08-month/isSameMonth": "https://codesandbox.io/s/04clz?previewwindow=tests&file=/08-month/isSameMonth/vanilla.js",
"date-fns/08-month/isThisMonth": "https://codesandbox.io/s/4u126?previewwindow=tests&file=/08-month/isThisMonth/vanilla.js",
"date-fns/08-month/setMonth": "https://codesandbox.io/s/jcvuk?previewwindow=tests&file=/08-month/setMonth/vanilla.js",
Expand Down
6 changes: 6 additions & 0 deletions src/content/date-fns/08-month/lastDayOfMonth/date-fns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// https://date-fns.org/v2.29.3/docs/lastDayOfMonth
const lastDayOfMonth = require('date-fns/lastDayOfMonth')

// Get the last day of a month.
module.exports = lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014
1 change: 1 addition & 0 deletions src/content/date-fns/08-month/lastDayOfMonth/notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Get the last day of a month from date.
9 changes: 9 additions & 0 deletions src/content/date-fns/08-month/lastDayOfMonth/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const expected = 'Fri Feb 28 2014'

const datefns = require('./date-fns')
const plain = require('./vanilla')

test('lastDayOfMonth', () => {
expect(datefns).toEqual(expected)
expect(plain).toEqual(datefns)
})
13 changes: 13 additions & 0 deletions src/content/date-fns/08-month/lastDayOfMonth/vanilla.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function lastDayOfMonth(date) {
const dateClone = new Date(date.getTime())
const month = dateClone.getMonth()
dateClone.setDate(1)
dateClone.setMonth(month + 1)
dateClone.setDate(0)

return dateClone
}

// Get the last day of a month.
module.exports = lastDayOfMonth(new Date(2014, 1, 25)).toDateString()
// => Fri Feb 28 2014

0 comments on commit aed8596

Please sign in to comment.