DayOfWeek


The DayOfWeek function computes the day of the week corresponding to a year, month, and day. The value is returned as a number 0 through 6 with Sunday being day 0.

Syntax

sDOW = DayOfWeek( nYear, nMonth, nDay )

where

    nYear, nMonth, nDay are the components of the date (e.g., 2007, 4, and 23).

    nDOW is the day of the week with values 0 through 6, beginning with Sunday = 0.

Example

The following script computes the day of the week and lists it in a nice format:

 

-- First, create a table contining the day names

DOW = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }

 

 

-- enter the year, month, and day to be evaluated

 

yr = 2009 ; mo = 1 ; day = 4

 

 

 

-- calculate the day of the week

 

nDay = DayOfWeek( yr, mo, day )

-- returns Sunday as 0

 

-- For printing the result, remember that Lua tables start at

-- index 1, but DayOfWeek() returns Sunday as 0, so we compensate

-- by fetching the day name from the table using index [nDay+1]

Printf("DOW for %04d/%02d/%02d = %d:%s\r\n", yr, mo, day, nDay, DOW[nDay+1] )

 

Related Topics

ParseYmd

FormatYmdStr

CalcJD

FormatDate