Home > FAQ > Data Manipulation > Date Rounding in Expressions > How many days in examples > How many days in a calendar year

How many days in a calendar year

There are 3 different ways to calculate the number of days in a calendar year: (1) determine if the year is a leap year (or not), (2) find the beginning and end of the current year, and (3) testing whether February has 28 or 29 days.

 

; A_DT is a single date or an array of date(s) that you need to
; adjust (e.g., DOB field or #DATE in a transformation expression)

 

; Method 1: ------------------------------------------
YR:= #YEAR A_DT &
DaysInYear:= #IF     0=(400 #MOD YR) ; divisible by 400?
#THEN 366 ; leap year
#ELSEIF 0=(100 #MOD YR) ; divisible by 100?
#THEN 365 ; not a leap year*
#ELSEIF 0=(4 #MOD YR)   ; divisible by 4?
#THEN 366 ; leap year
#ELSE 365 #ENDIF & ; not a leap year
; *Some versions of Excel think 1900 is a leap year (but it’s not)!

 

; Method 2: ------------------------------------------
BOY_DT:= (-12) #MONTHROUND A_DT &
; beginning of current calendar year
EOY_DT:= #ENDMTH (BOY_DT #DATEPLUS 360D) &
; end of current calendar year
DaysInYear:= 1 + EOY_DT – BOY_DT &

 

; Method 3: ------------------------------------------
; if Feb of the current year has 29 days, then it’s a leap year!
D_DT:= 3/1/1900 #DATEPLUS (((#YEAR A_DT)-1900)*1Y) &
; find 3/1 for the current year
DaysInYear:= 365 + (29=#DAY (D_DT #DATEMINUS 1D)) &
; if february has 29 days then it’s a leap year

 

; Method 4: ------------------------------------------
; if Feb of the current year has 29 days, then it’s a leap year!
D_DT:= 2/1/1900 #DATEPLUS (((#YEAR A_DT)-1900)*1Y) &
; find 2/1 for the current year
DaysInYear:=#IF 28=#DAYSINMTH D_DT ; 28 days in feb?
#THEN 365 ; NOT a leap year
#ELSE 366 #ENDIF & ; leap year