Year date functions VBA

This page displays code fragments for the following Year related calculations:

Number of day in year

Given a certain date, what is the number of day in year. For example dt = #3/18/2016# returns 78.

Dim iDayNumber As Integer
Dim dt As Date: dt = Date
iDayNumber = DateDiff("d", CDate("1/1/" & Year(dt)), dt) + 1

Is leap year

Nearly every four years is a leap year which has 366 days.

Dim booIsLeapYear As Boolean
Dim dt As Date: dt =
booIsLeapYear = ((Year(dt) Mod 4 = 0) And (Year(dt) Mod 100 <> 0)) Or (Year(dt) Mod 400 = 0)
YearbooIsLeapYear
2000True
2001False
2016True
1900False

Year - from date

The VBA function returns the year from a date. For example dt = #6/23/2016# returns 2016.

Dim dt As Date: dt =
Dim iYear As Integer
iYear = Year(dt)

First day in year

Given a certain date, what is the first day (date) in it's year. For example dt = #6/23/2016# returns 1/1/2016.

Dim dtFirstDayInYear As Date
Dim dt As Date: dt =
dtFirstDayInYear = DateSerial(Year(dt), 1, 1)

Last day in year

Given a certain date, what is the last day (date) in it's year. For example dt = #6/23/2016# returns 12/31/2016.

Dim dtLastDayInYear As Date
Dim dt As Date: dt =
dtLastDayInYear = DateSerial(Year(dt), 12, 31)