Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Corrected a solution for getting last day of the month #64

Merged
merged 1 commit into from

2 participants

@onlyjsmith

Have tested this in Chrome 23, Firefox 17 and Opera 12.

This SO page also relates http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript

The cookbook is a fantastic resource, btw!

onlyjsmith Solution was giving wrong answer
Have tested this in Chrome 23, Firefox 17 and Opera 12.

This SO page also relates http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript
c9d17ba
@amsul amsul merged commit e73ee06 into coffeescript-cookbook:master
@amsul
Collaborator

Yes, nicely spotted. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 23, 2012
  1. Solution was giving wrong answer

    onlyjsmith authored
    Have tested this in Chrome 23, Firefox 17 and Opera 12.
    
    This SO page also relates http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 chapters/dates_and_times/finding-last-day-of-the-month.md
View
4 chapters/dates_and_times/finding-last-day-of-the-month.md
@@ -13,9 +13,9 @@ Use JavaScript's Date underflow to find the -1th day of the following month:
{% highlight coffeescript %}
now = new Date
-lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, -1)
+lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)
{% endhighlight %}
## Discussion
-JavaScript's Date constructor cheerfully handles overflow and underflow conditions, which makes date math very easy. Given this ease of manipulation, it doesn't make sense to worry about how many days are in a given month; just nudge the math around. In December, the solution above will actually ask for the -1th day of the 13th month of the current year, which works out to the -1th day of January of NEXT year, which works out to the 31st day of December of the current year.
+JavaScript's Date constructor cheerfully handles overflow and underflow conditions, which makes date math very easy. Given this ease of manipulation, it doesn't make sense to worry about how many days are in a given month; just nudge the math around. In December, the solution above will actually ask for the 0th day of the 13th month of the current year, which works out to the day before the 1st day of January of NEXT year, which works out to the 31st day of December of the current year.
Something went wrong with that request. Please try again.