Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2: Added Year/Month support to DateAdd/DateDiff. #287

Open
wants to merge 1 commit into
base: alpha
Choose a base branch
from

Conversation

jeeswg
Copy link
Contributor

@jeeswg jeeswg commented Jul 5, 2022

Documentation

For the DateAdd and DateDiff functions:
Specify a TimeUnits string that starts with Mo to add/compare months.
Specify a TimeUnits string that starts with Y to add/compare years.
Any TimeUnits string that starts with M, but doesn't start with Mo, adds/compares minutes.

DateAdd handling for days 29/30/31 of a month for Mo and Y:
Where an equivalent day of the month does not exist, DateAdd always 'rounds downs' to the nearest extant day. The logic is similar to that of SysDateTimePick32 controls.
E.g. 19990331 + 1 month 'rounds down' to 19990430.
E.g. 20000229 + 1 year 'rounds down' to 20010228.

DateDiff comparison with months and years:
When comparing months, DateDiff compares the yyyyMM portions to get an initial complete month count, it then compares ddHHmmss portions to determine whether or not to reduce the count by 1.
When comparing years, DateDiff compares the yyyy portions to get an initial complete year count, it then compares MMddHHmmss portions to determine whether or not to reduce the count by 1.

Test code

;==================================================

;test code: DateAdd/DateDiff (Year/Month support) (AHK v2)

;==================================================

Assert(DateAdd(20220131, 1, "Mo"), 20220228000000)
Assert(DateAdd(20220131, 2, "Mo"), 20220331000000)
Assert(DateAdd(20220131, 3, "Mo"), 20220430000000)
Assert(DateAdd(20220131, 4, "Mo"), 20220531000000)

Assert(DateAdd(20220131, 11, "Mo"), 20221231000000)
Assert(DateAdd(20220131, 12, "Mo"), 20230131000000)
Assert(DateAdd(20220131, 24, "Mo"), 20240131000000)
Assert(DateAdd(20220131, -12, "Mo"), 20210131000000)

Assert(DateAdd(20220131, 1, "M"), 20220131000100)

Assert(DateAdd(20000229, 1, "Y"), 20010228000000)
Assert(DateAdd(20000229, 2, "Y"), 20020228000000)
Assert(DateAdd(20000229, 3, "Y"), 20030228000000)
Assert(DateAdd(20000229, 4, "Y"), 20040229000000)
Assert(DateAdd(20000229, 5, "Y"), 20050228000000)

;day count for each month (2001) (365-day year):
vDate := DateAdd(2001, -1, "D")
vOutput := ""
Loop 12
{
	vDate2 := DateAdd(vDate, A_Index, "Mo")
	vOutput .= (A_Index=1?"":",") FormatTime(vDate2, "dd")
}
Assert(vOutput, "31,28,31,30,31,30,31,31,30,31,30,31")

;day count for each month (2000) (leap year):
vDate := DateAdd(2000, -1, "D")
vOutput := ""
Loop 12
{
	vDate2 := DateAdd(vDate, A_Index, "Mo")
	vOutput .= (A_Index=1?"":",") FormatTime(vDate2, "dd")
}
Assert(vOutput, "31,29,31,30,31,30,31,31,30,31,30,31")

;==================================================

Assert(DateDiff(20000101, 20000601, "Y"), 0)
Assert(DateDiff(20000601, 20000101, "Y"), 0)

Assert(DateDiff(20000101, 20000116, "Mo"), 0)
Assert(DateDiff(20000116, 20000101, "Mo"), 0)

Assert(DateDiff(20100101, 20000101, "Y"), 10)
Assert(DateDiff(20100101, 20000101, "Mo"), 120)
Assert(DateDiff(20100101, 2000010101, "Y"), 9)
Assert(DateDiff(20100101, 2000010101, "Mo"), 119)

Assert(DateDiff(20000101, 20100101, "Y"), -10)
Assert(DateDiff(20000101, 20100101, "Mo"), -120)
Assert(DateDiff(2000010101, 20100101, "Y"), -9)
Assert(DateDiff(2000010101, 20100101, "Mo"), -119)

;==================================================

MsgBox("done")
return

;==================================================

Assert(vValue1, vValue2)
{
	if (vValue1 != vValue2)
		throw Error("Assert mismatch.`r`n" "Return value: " vValue1 "`r`n" "Expected value: " vValue2, -1)
}

;==================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant