﻿ Year date functions VBA

Year date functions VBA

Number of day in year

Given a certain date, what is the number of day in year. For example `dt = #3/18/2019#` 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)``````
 Year booIsLeapYear 2000 True 2001 False 2019 True 1900 False

Year - from date

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

``````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/2019#` returns 1/1/2019.

``````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/2019#` returns 12/31/2019.

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