Date and Time Functions

CDATEReturns the expression converted to a date data type (Date).
CVDATEReturns the expression converted to a variant data type (Variant).
DATE - FunctionReturns the current system date (Date).
DATE - StatementDefines the current system date.
DATEADDReturns the date to which a specified time interval has been added (Date).
DATEDIFFReturns the number of time intervals between two specified dates (Long).
DATEPARTReturns the specified part of a given date (Integer).
DATESERIALReturns the date given a year, month and day (Date).
DATEVALUEReturns the date given a string representation of a date (Date).
DAYReturns the day from a given date (Integer).
FORMATReturns the text string of a number or date in a particular format (String).
FORMATDATETIMEReturns the expression formatted as a date or time (String).
HOURReturns the hour from a given time (Integer).
INTReturns the integer portion of a number (Double).
ISDATEReturns the value indicating if an expression is a date (Boolean).
MINUTEReturns the minute from a given time (Integer).
MONTHReturns the month from a given date (Integer).
MONTHNAMEReturns the month as a string (String).
NOWReturns the current system date and time (Date).
SECONDReturns the second from a given time (Integer).
TIME - FunctionReturns the current system time (Date).
TIME - StatementDefines the current system time.
TIMERReturns the number of seconds elapsed since midnight (Single).
TIMESERIALReturns the time for a specific hour, minute and second (Date).
TIMEVALUEReturns the time given a string representation of a time (Date).
WEEKDAYReturns the number representing the day of the week for a given date (Integer).
WEEKDAYNAMEReturns the day of the week as a string (String).
YEARReturns the year from a date (Integer).
CDATE
Returns the expression converted to a date data type (Date).
CVDATE
Returns the expression converted to a variant data type (Variant).
DATE - Function
Returns the current system date (Date).
DATE - Statement
Defines the current system date.
DATEADD
Returns the date to which a specified time interval has been added (Date).
DATEDIFF
Returns the number of time intervals between two specified dates (Long).
DATEPART
Returns the specified part of a given date (Integer).
DATESERIAL
Returns the date given a year, month and day (Date).
DATEVALUE
Returns the date given a string representation of a date (Date).
DAY
Returns the day from a given date (Integer).
FORMAT
Returns the text string of a number or date in a particular format (String).
FORMATDATETIME
Returns the expression formatted as a date or time (String).
HOUR
Returns the hour from a given time (Integer).
INT
Returns the integer portion of a number (Double).
ISDATE
Returns the value indicating if an expression is a date (Boolean).
MINUTE
Returns the minute from a given time (Integer).
MONTH
Returns the month from a given date (Integer).
MONTHNAME
Returns the month as a string (String).
NOW
Returns the current system date and time (Date).
SECOND
Returns the second from a given time (Integer).
TIME - Function
Returns the current system time (Date).
TIME - Statement
Defines the current system time.
TIMER
Returns the number of seconds elapsed since midnight (Single).
TIMESERIAL
Returns the time for a specific hour, minute and second (Date).
TIMEVALUE
Returns the time given a string representation of a time (Date).
WEEKDAY
Returns the number representing the day of the week for a given date (Integer).
WEEKDAYNAME
Returns the day of the week as a string (String).
YEAR
Returns the year from a date (Integer).

Age

Excel user defined function - AGE


FirstDayOfTheMonth

Excel user defined function - DATEFIRST


LastDayOfTheMonth

Excel user defined function - DATELAST


Pause

This Pause for 5 seconds

Nexttime = Now() + TimeValue("00:00:05") 
Application.Wait Nexttime

IsInThePast

Check if a date is in the past

Public Function IsInThePast(ByVal dtDate As Date) As Boolean 
   If (dtDate < VBA.Date()) Then
      IsInThePast = True
   Else
      IsInThePast = False
   End If
End Function

IsWorkday

This function determines whether a date falls on a weekday. If no date passed in, use today's date.
vbMonday = 2, vbSaturday = 7, vbSunday = 1

Public Function IsWorkday(Optional ByVal dteDate As Date) As Boolean 

If CLng(dteDate) = 0 Then
dteDate = Date
End If

Select Case Weekday(dteDate)
Case vbMonday To vbFriday
IsWorkday = True
Case Else
IsWorkday = False
End Select
End Function

IsLeapYear

=VBA.Year(dtDate) Mod 4 = 0) And _ 
   (VBA.Year(dtDate) Mod 100 <> 0) Or (VBA.Year(dtDate) Mod 400 = 0))

WhichQuarter

Public Function WhichQuarter(ByVal dtDate As Date) As Integer 
Dim quarter As Integer
   WhichQuarter = VBA.Int((VBA.Month(VBA.Date) -1) / 3) + 1
End Function

Time Loop

Public Sub TimeLoop() 
   Dim AbortTime As Date
   
   AbortTime = Now + TimeValue("0:00:05") ' Wait for 5 seconds

   Do While (True)
'Code for one iteration goes here
      If (Now > AbortTime) Then
         Exit Do
      End If
   Loop
End Sub

Anniversary

The following procedure finds the anniversary of a given date; that is, the next date on which it occurs.
If the anniversary has already occurred this year, the procedure returns the date of the anniversary in the next year.
This function finds the next anniversary of a date. If the date has already passed for this year, it returns the date on which the anniversary occurs in the following year.
The procedure determines the last day of the last year by using the DateSerial function, and then subtracts that date from the date that was passed in to the procedure.

Public Function Anniversary(ByVal dteDate As Date) As Date 
Dim dteThisYear As Date

' Find corresponding date this year.
dteThisYear = DateSerial(Year(Date), Month(dteDate), Day(dteDate))
' Determine whether it's already passed.
If dteThisYear < Date Then
Anniversary = DateAdd("yyyy", 1, dteThisYear)
Else
Anniversary = dteThisYear
End If
End Function

DayOfTheYear

This function takes a date as an argument and returns the day number for that year.
If the dteDate argument is omitted, the function uses the current date.
Using the DateDiff function to determine the number of years between today and a birthdate doesn't always give a valid result because the DateDiff function rounds to the next year.
If a person's birthday hasn't yet occurred, using the DateDiff function will make the person one year older than he or she actually is.
To remedy this situation, the procedure checks to see whether the birthday has already occurred this year, and if it hasn't, it subtracts 1 to return the correct age.

Public Function DayNumberInYear(Optional ByVal dteDate As Date) As Long 

' If dteDate argument has not been passed, dteDate is
' initialized to 0 (or December 30, 1899, the date equivalent of 0

If CLng(dteDate) = 0 Then
dteDate = VBA.Date()
End If

' Calculate the number of days that have passed since
' December 31 of the previous year.
DayOfYear = Abs(DateDiff("d", dteDate, VBA.DateSerial(Year(dteDate) - 1, 12, 31)))

End Function

© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext