Stuart McCall's Microsoft Access Pages - Dates

Home
Determine number of days in the month of given date

Public Function DaysInMonth(dt As Date) As Integer
'Add one month, subtract dates to find difference.
    DaysInMonth = DateSerial(Year(dt), Month(dt) + 1, Day(dt)) _
                - DateSerial(Year(dt), Month(dt), Day(dt))
End Function
Paste this Code into a standard module.
Home Contents

Determine first day in the month of given date

Public Function FirstMonthDay(dt As Date) As Date
'Returns the date of the first day in the month of dt
    FirstMonthDay = DateSerial(Year(dt), Month(dt), 1)
End Function
Paste this Code into a standard module.
Home Contents

Determine last day in the month of given date

Public Function LastMonthDay(dt As Date) As Date
'Returns the date of the last day in the month of dt
    LastMonthDay = DateSerial(Year(dt), Month(dt) + 1, 0)
End Function
Paste this Code into a standard module.
Home Contents

Determine the date of the first day in the quarter of given date

Public Function FirstQuarterDay(dt As Date) As Date
'Returns the date of the first day in the quarter of dt
    FirstQuarterDay = DateSerial(Year(dt), Int((Month(dt) - 1) / 3) * 3 + 1, 1)
End Function
Paste this Code into a standard module.
Home Contents

Determine the date of the last day in the quarter of given date

Public Function LastQuarterDay(dt As Date) As Date
'Returns the date of the last day in the quarter of dt
    LastQuarterDay = DateSerial(Year(dt), Int((Month(dt) - 1) / 3) * 3 + 4, 0)
End Function
Paste this Code into a standard module.
Home Contents

Determine the number of days between startDate and endDate

Public Function ElapsedDays(StartDate As Date, EndDate As Date) As Long
'Returns the number of days between startDate and endDate
    ElapsedDays = Int(CSng(EndDate - StartDate))
End Function
Paste this Code into a standard module.
Home Contents

Format a date with numeric suffixes (eg 3rd September 2007)

Public Function DateFormat(dt As Date, Optional Fmt As String = "dd~ mmmm yyyy") As String
'Returns a date formatted like:
'
'    DateFormat("1-Sep-99") - "01st September 1999"
'    DateFormat("3-Sep-99", "d~ mmmm yyyy") - "3rd September 1999"
'    DateFormat("5-Sep-99", "d~") - "5th"
'
'(in other words, put the tilde ~ where the suffix is to appear)
    Dim s As String
    '
    Select Case Day(dt)
        Case 1, 21, 31
            s = "st"
        Case 2, 22
            s = "nd"
        Case 3, 23
            s = "rd"
        Case 4 To 20, 24, 25 To 30
            s = "th"
    End Select
    DateFormat = Replace(Format(dt, Fmt), "~", s)
End Function
Paste this Code into a standard module.
Home Contents

Determine age in years between 2 dates

Public Function Age(Bdate As Date, DateToday As Date) As Integer
'Returns age in years between 2 dates
    If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
      Age = Year(DateToday) - Year(Bdate) - 1
    Else
      Age = Year(DateToday) - Year(Bdate)
    End If
End Function
Paste this Code into a standard module.
Home Contents

Determine the quarter no. of the given date

Public Function Which_Quarter(dt As Date) As Integer
'Returns the quarter no. of the date dt
    Which_Quarter = DatePart("q", dt)
End Function
Paste this Code into a standard module.
Home Contents

Determine the week no. (from start of quarter) of the given date

Public Function Which_Week(dt As Date) As Integer
'Returns the week no. (from start of quarter) of the date dt
    Dim w As Integer
    '
    w = DatePart("ww", dt)
    Select Case w
        Case 1 To 13
            Which_Week = w
        Case 14 To 26
            Which_Week = w - 13
        Case 27 To 39
            Which_Week = w - 26
        Case 40 To 52
            Which_Week = w - 39
        Case Else
    End Select
End Function
Paste this Code into a standard module.
Home Contents