Get Day Number (code snippet)

Description

The fh function =DayNumber(Date) or Plugin function fhCallBuiltInFunction("DayNumber",datDate) should return the Day Number for any given date point, but it returns nil in the following abnormal cases.

  • Any day of month greater than last day of month
  • Any month number greater than 12
  • Skipped dates from 5 - 14 Oct 1582 or from 3 - 13 Sep 1752
  • Any BC dates
  • Any Julian, Hebrew, or French calendar dates
  • When the date point parameter is null

This function caters for all date points, and reports the abnormal cases, substituting a rational alternative.

Requires: None

The fh function =DayOfWeek(Date) or Plugin function fhCallBuiltInFunction("DayOfWeek",datDate) similarly returns an empty string instead of a weekday for the abnormal cases above, and could be corrected by a function based on the code below.

Code

GetDayNumber.fh_lua
-- Obtain the Day Number for any Date Point --
function GetDayNumber(datDate)
	local intDay = fhCallBuiltInFunction("DayNumber",datDate) -- Only works for Gregorian dates that were not skipped nor BC dates
	if not intDay then
		local intMonth = math.min( datDate:GetMonth(), 12 ) -- Limit month to 12, and day to last of each month
		local intDayNo = math.min( datDate:GetDay(), ({0;31;28;31;30;31;30;31;31;30;31;30;31;})[intMonth+1] )
		local intYear  = datDate:GetYear()
		if datDate:GetCalendar() == "Hebrew" and intYear > 3761 then intYear = intYear - 3761 end
		if     intYear == 1752 and intMonth ==  9 then intDayNo = 2 -- Use 2 Sep 1752 for 3 - 13 Sep 1752 skipped dates
		elseif intYear == 1582 and intMonth == 10 then intDayNo = 4 -- Use 4 Oct 1582 for 5 - 14 Oct 1582 skipped dates
		end	
		local setDate = fhNewDatePt(intYear,intMonth,intDayNo,datDate:GetYearDD())
		intDay = fhCallBuiltInFunction("DayNumber",setDate) 	-- Remove BC and Julian, Hebrew, French calendars
		if not intDay then intDay = 0 end
		local oldDate = fhNewDate()		oldDate:SetSimpleDate(datDate)	-- Report problem to user
		local newDate = fhNewDate()		newDate:SetSimpleDate(setDate)
		local strIsBC = ""
		if datDate:GetBC() then intDay = -intDay	strIsBC = "and Day Number negated" end
		fhMessageBox("\n Get Day Number issue for \n "..oldDate:GetDisplayText().." \n So replaced it with \n "..newDate:GetDisplayText().." \n "..strIsBC)
	end
	return intDay
end -- function GetDayNumber

Usage

local intDay = GetDayNumber(fhNewDatePt(1752,9,10))
print(intDay)
639798

and produces the message:

  Get Day Number issue for
  10 September 1752
  So replaced it with
  2 September 1752


The following example provides a Day Number for any simple, period, range, or quarter date.

local datPt1 = fhNewDatePt()
local datPt2 = fhNewDatePt()
local datDay = fhNewDate()
datDay:SetValueAsText("Between 1800 and 1900")
datPt1= datDay:GetDatePt1()
datPt2= datDay:GetDatePt2()
if datPt2:IsNull() then datPt2 = datPt1 end
local intDay = ( GetDayNumber(datPt1) + GetDayNumber(datPt2) ) / 2
print(intDay)
675336	-- equals the Day Number for 1850