Date Functions

Blitz3D Forums/Blitz3D Beginners Area/Date Functions

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I think this will do it. I can't think of any test cases where you might need to add more than one month worth of days to the delta date, but it might be worth thinking about further.

Function weirdDateDiff$(d1, m1, y1, d2, m2, y2)
	Local dy = y2 - y1
	Local dm = m2 - m1
	Local dd = d2 - d1
	If dd < 0 Then
		dd = dd + numberOfDaysInMonth(m2, y2)
		dm = dm - 1
	EndIf
	If dm < 0 Then
		dm = dm + 12
		dy = dy - 1
	EndIf
	If dy < 0 Then RuntimeError("date1 > date2")
	Return dy + " years, " + dm + " months, and " + dd + " days"
End Function

Function numberOfDaysInMonth(m, y)
	If m < 1 Or m > 12 Then RuntimeError("invalid month")
	If m = 2 Then
		Local isLeap = ( y Mod 4 = 0 ) And (( y Mod 100 <> 0 ) Or ( y Mod 400 = 0 ))
		If isLeap Then Return 29 Else Return 28
	ElseIf (m < 8 And m Mod 2 = 1) Or (m >=8 And m Mod 2 = 0)
		Return 31
	Else
		Return 30
	EndIf
End Function