Number of days in a year

X

XtraNeed

I need a macro function that allows me to display the number of days (or
hours) in a given year. Any help will be appreciated.
 
H

Harald Staff

Two approaches. The first subtracts start from end date, the second use leap
year rules:

Function DaysinYear(LngYear As Long) As Long
DaysinYear = DateSerial(LngYear, 12, 31) - DateSerial(LngYear, 1, 1) + 1
End Function

Function DaysinYear2(LngYear As Long) As Long
DaysinYear2 = 365
If LngYear Mod 4 = 0 Then DaysinYear2 = 366
If LngYear Mod 100 = 0 Then DaysinYear2 = 365
If LngYear Mod 400 = 0 Then DaysinYear2 = 366
End Function

Sub test()
Dim L As Long
For L = 1998 To 2005
MsgBox DaysinYear(L) & vbNewLine & _
DaysinYear2(L), , L
Next
End Sub

HTH. best wishes Harald
 
D

Dave Peterson

dim myYear as long
dim daysinyear as long

myyear = 2005

if month(dateserial(myYear,2,29))= 2 then
daysinyear = 366
else
daysinyear = 365
end if
 
H

Harald Staff

Ah, month. Another just for the fun of it:

Function DaysinYear3(LngYear As Long) As Long
DaysinYear3 = 368 - Month(DateSerial(LngYear, 2, 29))
End Function

Best wishes Harald
 
D

Dana DeLouis

Just another method:

Function DaysInYear(Year) As Long
DaysInYear = Format(DateSerial(Year, 12, 31), "y")
End Function
 
Top