Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript finds the last day of the month


May 09, 2021 CoffeeScript


Table of contents


Find the last day of the month

Problem

You need to find out the last day of the month, but there is no fixed schedule for each month of the year.

Solution

Use JavaScript's date overflow to find the first day of the month:

now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)

Discuss

JavaScript's date constructor successfully handles overflows and underflows, making the calculation of dates simple. G iven this simple operation, there is no need to worry about how many days there are in a given month; In December, the above solution is to look for the date of the 0th day of the 13th month of the current year, then it is January 1 of the following year, and the date of December 31st of this year is calculated.