Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Determining the Number of Days in a Month with Javascript

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!

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:

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}


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)

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."

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.

<button onclick="alert(daysInMonth(1, 2100));">February 2100</button>
<button onclick="alert(daysInMonth(1, 2005));">February 2005</button>
<button onclick="alert(daysInMonth(1, 2004));">February 2004</button>
<button onclick="alert(daysInMonth(1, 2003));">February 2003</button>
<button onclick="alert(daysInMonth(1, 2001));">February 2001</button>
<button onclick="alert(daysInMonth(1, 2000));">February 2000</button>
<button onclick="alert(daysInMonth(1, 1999));">February 1999</button>


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!

work days between two dates without cycling through dates

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.

4GL Version
 
# workdays
# returns the number of working days between two dates
FUNCTION workdays( dt_begin, dt_end )

DEFINE
   dt_begin             DATE,
   dt_end               DATE,
   dt_first_sunday      DATE,
   dt_last_saturday     DATE,
   int_workdays         INTEGER

   # get first sunday
   LET dt_first_sunday = dt_begin + ((7 - WEEKDAY(dt_begin)) MOD 7)

   # get last saturday
   LET dt_last_saturday = dt_end + ((-1 * (WEEKDAY(dt_end) + 1)) MOD 7)

   # get work weeks between first sunday and last saturday
   LET int_workdays = (((dt_last_saturday - dt_first_sunday) + 1) / 7) * 5
   
   # if first sunday is not begin date
   IF dt_first_sunday <> dt_begin THEN

      # assume first sunday is after begin date
      # add workdays from begin date to first sunday
      LET int_workdays = int_workdays + (6 - WEEKDAY(dt_begin))

   END IF

   # if last saturday is not end date
   IF dt_last_saturday <> dt_end THEN

      # assume last saturday is before end date
      # add workdays from last saturday to end date
      LET int_workdays = int_workdays + WEEKDAY(dt_end)  
  
   END IF

   # return working days
   RETURN int_workdays

END FUNCTION


VBA Version
' WorkDays
' returns the number of working days between two dates
Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long

   Dim dtFirstSunday As Date
   Dim dtLastSaturday As Date
   Dim lngWorkDays As Long

   ' get first sunday in range
   dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)

   ' get last saturday in range
   dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)

   ' get work days between first sunday and last saturday
   lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5

   ' if first sunday is not begin date
   If dtFirstSunday <> dtBegin Then

      ' assume first sunday is after begin date
      ' add workdays from begin date to first sunday
      lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))

   End If

   ' if last saturday is not end date
   If dtLastSaturday <> dtEnd Then

      ' assume last saturday is before end date
      ' add workdays from last saturday to end date
      lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)

   End If

   ' return working days
   WorkDays = lngWorkDays

End Function
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS