dayofweek

function dayofweek( year, month, day ) --> dow

Description

Returns the day of the week corresponding to a date. The result is given as an integer from 1(Monday) to 7(Sunday).

Parameters

year

The year as a number.

month

The month as a number from 1(January) to 12(December).

day

The day as a number from 1 to 31

Return Values

dow

The weekday as integer ranging from 1 (Monday) to 7 (Sunday).

Code

--ZFUNC-dayofweek-v1
local function dayofweek( year, month, day ) --> dow
   local century = function( year ) return math.floor( year / 100 ) end

   --ZFUNC-isleapyear-v1
   local isleapyear = function( year )
      if( year % 4 == 0 and year % 100 ~= 0 ) or ( year % 400 == 0 ) then
         return true
      else
         return false
      end
   end

   local month_number_for_month = { 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }

   local day_num = day % 7
   local month_num = month_number_for_month[ month ]

   local y = year - century( year ) * 100
   local year_num = ( y + math.floor( y / 4 ) ) % 7

   local century_num = ( 3 - ( century( year ) % 4 ) ) * 2

   local leap_num = 0
   if ( isleapyear( year ) and ( month < 3 ) ) then leap_num = 6 end

   local res = day_num + month_num + year_num + century_num + leap_num - 1
   return ( res % 7 ) + 1
end

return dayofweek

Examples

local t = require( "taptest" )
local dayofweek = require( "dayofweek" )

t( dayofweek( 2012, 4, 8 ), 7 ) -- Sunday
t( dayofweek( 1949, 5, 23 ), 1 ) -- Monday
t( dayofweek( 1789, 7, 14 ), 2 ) -- Tuesday
t( dayofweek( 1892, 1, 18 ), 1 ) -- Monday
t( dayofweek( 1989, 11, 9 ), 4 ) -- Thursday

t()

See also