<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: days code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 18:56:52 GMT</pubDate>
    <description>DZone Snippets: days code</description>
    <item>
      <title>Determining the Number of Days in a Month with Javascript</title>
      <link>http://snippets.dzone.com/posts/show/2099</link>
      <description>Essentially, writing some code to determine the number of days in a given month of a given year with javascript is not the worlds most difficult task. It is the type of exercise that one would expect to be given as a newbie developer during a lab or lecture. The solution normally involves determining if the month is February, an month with 30 days or a month with 31 days, then (if February) checking if the year is a leap year. All these tests add up, however, and add several lines of code to your .js file. They are also unnecessary!&lt;br /&gt;&lt;br /&gt;Apparently, the javascript Date function allows you to overflow the day number parameter that you pass, creating a date in the next month. Deliberately overflowing the day parameter and checking how far the resulting date overlaps into the next month is a quick way to tell how many days there were in the queried month. Here is a function that does this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function daysInMonth(iMonth, iYear)&lt;br /&gt;{&lt;br /&gt;	return 32 - new Date(iYear, iMonth, 32).getDate();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;N.B.: iMonth is zero based, so 0 represents January, 1 represents February, 2 represents March and 11 represents December. iYear is not zero based, this is the actual calendar year number. (2006 is actually 2006)&lt;br /&gt;&lt;br /&gt;Pray that the browser developers know the correct way to determine whether a year is a leap year! (It's more complicated than a simple mod 4 == 0) Here is a quote from Wikipedia's page on leap years: "The Gregorian calendar, the current standard calendar in most of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in '00'), which receive the extra day only if they are evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900 and 2100 are not."&lt;br /&gt;&lt;br /&gt;To test this function, February in the years 2100, 2005, 2004, 2003, 2001, 2000 and 1999 should be checked. All of these should return 28, except for 2004 and 2000.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2100));"&gt;February 2100&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2005));"&gt;February 2005&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2004));"&gt;February 2004&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2003));"&gt;February 2003&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2001));"&gt;February 2001&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 2000));"&gt;February 2000&lt;/button&gt;&lt;br /&gt;&lt;button onclick="alert(daysInMonth(1, 1999));"&gt;February 1999&lt;/button&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;How does this function work? It is quite simple. When the Date() function is given a day number that is greater than the number of days in the given month of the given year, it wraps the date into the next month. The getDate() function returns the day of the month, starting from the beginning of the month that the date is in. So, day 32 of March is considered to be day 1 of April. Subtracting 1 from 32 gives the correct number of days in March!</description>
      <pubDate>Thu, 25 May 2006 12:02:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2099</guid>
      <author>Charlie (Stephen Martindale)</author>
    </item>
    <item>
      <title>work days between two dates without cycling through dates</title>
      <link>http://snippets.dzone.com/posts/show/808</link>
      <description>I was thinking about how to optimize figuring out the work days between two dates and came up with this function.  It doesn't take into account holidays.  You would have to take out the workdays for holidays from the number if you want to take into account holidays, but that should be easy enough.&lt;br /&gt;&lt;br /&gt;4GL Version&lt;br /&gt;&lt;code&gt; &lt;br /&gt;# workdays&lt;br /&gt;# returns the number of working days between two dates&lt;br /&gt;FUNCTION workdays( dt_begin, dt_end )&lt;br /&gt;&lt;br /&gt;DEFINE&lt;br /&gt;   dt_begin             DATE,&lt;br /&gt;   dt_end               DATE,&lt;br /&gt;   dt_first_sunday      DATE,&lt;br /&gt;   dt_last_saturday     DATE,&lt;br /&gt;   int_workdays         INTEGER&lt;br /&gt;&lt;br /&gt;   # get first sunday&lt;br /&gt;   LET dt_first_sunday = dt_begin + ((7 - WEEKDAY(dt_begin)) MOD 7)&lt;br /&gt;&lt;br /&gt;   # get last saturday&lt;br /&gt;   LET dt_last_saturday = dt_end + ((-1 * (WEEKDAY(dt_end) + 1)) MOD 7)&lt;br /&gt;&lt;br /&gt;   # get work weeks between first sunday and last saturday&lt;br /&gt;   LET int_workdays = (((dt_last_saturday - dt_first_sunday) + 1) / 7) * 5&lt;br /&gt;   &lt;br /&gt;   # if first sunday is not begin date&lt;br /&gt;   IF dt_first_sunday &lt;&gt; dt_begin THEN&lt;br /&gt;&lt;br /&gt;      # assume first sunday is after begin date&lt;br /&gt;      # add workdays from begin date to first sunday&lt;br /&gt;      LET int_workdays = int_workdays + (6 - WEEKDAY(dt_begin))&lt;br /&gt;&lt;br /&gt;   END IF&lt;br /&gt;&lt;br /&gt;   # if last saturday is not end date&lt;br /&gt;   IF dt_last_saturday &lt;&gt; dt_end THEN&lt;br /&gt;&lt;br /&gt;      # assume last saturday is before end date&lt;br /&gt;      # add workdays from last saturday to end date&lt;br /&gt;      LET int_workdays = int_workdays + WEEKDAY(dt_end)  &lt;br /&gt;  &lt;br /&gt;   END IF&lt;br /&gt;&lt;br /&gt;   # return working days&lt;br /&gt;   RETURN int_workdays&lt;br /&gt;&lt;br /&gt;END FUNCTION&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;VBA Version&lt;br /&gt;&lt;code&gt;&lt;br /&gt;' WorkDays&lt;br /&gt;' returns the number of working days between two dates&lt;br /&gt;Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long&lt;br /&gt;&lt;br /&gt;   Dim dtFirstSunday As Date&lt;br /&gt;   Dim dtLastSaturday As Date&lt;br /&gt;   Dim lngWorkDays As Long&lt;br /&gt;&lt;br /&gt;   ' get first sunday in range&lt;br /&gt;   dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)&lt;br /&gt;&lt;br /&gt;   ' get last saturday in range&lt;br /&gt;   dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)&lt;br /&gt;&lt;br /&gt;   ' get work days between first sunday and last saturday&lt;br /&gt;   lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5&lt;br /&gt;&lt;br /&gt;   ' if first sunday is not begin date&lt;br /&gt;   If dtFirstSunday &lt;&gt; dtBegin Then&lt;br /&gt;&lt;br /&gt;      ' assume first sunday is after begin date&lt;br /&gt;      ' add workdays from begin date to first sunday&lt;br /&gt;      lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))&lt;br /&gt;&lt;br /&gt;   End If&lt;br /&gt;&lt;br /&gt;   ' if last saturday is not end date&lt;br /&gt;   If dtLastSaturday &lt;&gt; dtEnd Then&lt;br /&gt;&lt;br /&gt;      ' assume last saturday is before end date&lt;br /&gt;      ' add workdays from last saturday to end date&lt;br /&gt;      lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)&lt;br /&gt;&lt;br /&gt;   End If&lt;br /&gt;&lt;br /&gt;   ' return working days&lt;br /&gt;   WorkDays = lngWorkDays&lt;br /&gt;&lt;br /&gt;End Function&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Oct 2005 23:24:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/808</guid>
      <author>Will_Rickards (Will Rickards)</author>
    </item>
  </channel>
</rss>
