logo VB.Net - Other Date Functions
Guide Contents
DigitalDan Home Page

Other Date Functions
This page includes a set of date related functions which can be useful but do not fit into the normal date calculation categories. As with all date related calcularions, you may need to be aware of time-zone, summer-time and regional computer settings.
 
Assorted Date Related Functions
Private Shared Function Get_Todays_Date() As Date
Return Now
End Function
'
Private Function Is_Leap_Year(yyyy as integer) As Boolean
Return Is_Leap_Year(yyyy)
End Function
'
Private Function Set_To_Midday(dat As Date) As Date
Return New Date(dat.Year, dat.Month, dat.Day, 12, 0, 0)
' setting time before undertaking certain date calculations
' will reduce time-zone and daylight-saving complications
End Function
'
Private Function Days_Between_Two_Dates(dat1 As Date, dat2 As Date) As Integer
dat1 = New Date(dat1.Year, dat1.Month, dat1.Day, 12, 0, 0)
dat2 = New Date(dat2.Year, dat2.Month, dat2.Day, 12, 0, 0)
Return CInt(Math.Round(DateDiff(DateInterval.Day, dat1, dat2)))
' notice setting times to midday and rounding result - this avoids daylight saving time issues
End Function
'
Private Function Is_Valid_Date(yyyy As Integer, mm As Integer, dd As Integer) As Boolean
If yyyy < 2000 Or yyyy > 2999 Or mm < 1 Or mm > 12 Or dd < 1 Then Return False
Dim maxdays() As Integer = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
If dd > maxdays(mm) Then Return False
If dd <> 29 Or mm <> 2 Then Return True
Return (New Date(yyyy, 2, 28, 12, 0, 0).AddDays(1).Month = 2)
' VB function has an IsDate function
' This version bypasses some regional date format variations
End Function
'
Private Function Get_Day_Name(dat As Date, Optional shortName As Boolean = True) As String
' uses regional settings -English could return Monday and French could return Lundy
If shortName Then Return dat.ToString("ddd")
Return dat.ToString("dddd")
End Function
'
Private Function Get_Month_Name(dat As Date, Optional shortName As Boolean = True) As String
' uses regional settings -English could return January and French could return Janvier
If shortName Then Return dat.ToString("MMM")
Return dat.ToString("MMMM")
End Function
'
Private Shared Function Get_Ordinal(num As Integer) As String
' This is based on English numbers. Will need adjusting for other languages
If ((num Mod 100 >= 11) And (num Mod 100 <= 13)) Then Return "th"
Select Case num Mod 10
Case 1 : Return "st"
Case 2 : Return "nd"
Case 3 : Return "rd"
End Select
Return "th"
End Function
'
Private Shared Function First_Day_in_Month(yyyy As Integer, mm As Integer, dow As DayOfWeek) As Date
' may need adapting for non-English languages
' may need adapting for non-Gregorian calendars
Dim day1 As Integer = 1
' set to 1 for 1st occurence of weekday in month
' set to 8 for 2nd occurence in month
' set to 15 for 3rd occurence in month
' set to 22 for 4th occurence in month
Dim dat As New Date(yyyy, mm, day1, 12, 0, 0, 0)
While dat.DayOfWeek <> dow
dat = dat.AddDays(1)
End While
Return dat
End Function
'
Private Shared Function Last_Day_in_Month(yyyy As Integer, mm As Integer, dow As DayOfWeek) As Date
' find last day of month
mm += 1
If mm > 12 Then mm = 1 : yyyy += 1
Dim dat As New Date(yyyy, mm, 1, 12, 0, 0, 0)
dat = dat.AddDays(-1)
While dat.DayOfWeek <> dow
dat = dat.AddDays(-1)
End While
Return dat
End Function
'
Private Shared Function isWeekday(dat As Date) As Boolean
' this does not check for bank holidays
' may need adapting for non-English languages
' may need adapting for non-Gregorian calendars
Return Not ((dat.DayOfWeek = DayOfWeek.Saturday) Or (dat.DayOfWeek = DayOfWeek.Saturday))
End Function
'
Private Shared Function isWeekend(dat As Date) As Boolean
' this does not check for bank holidays
' may need adapting for non-English languages
' may need adapting for non-Gregorian calendars
Return ((dat.DayOfWeek = DayOfWeek.Saturday) Or (dat.DayOfWeek = DayOfWeek.Saturday))
End Function
'
Private Shared Function isWeekday(dat As Date) As Boolean
' this does not check for bank holidays
' may need adapting for non-English languages
' may need adapting for non-Gregorian calendars
Return Not ((dat.DayOfWeek = DayOfWeek.Saturday) Or (dat.DayOfWeek = DayOfWeek.Saturday))
End Function
'
Private Shared Function isWeekend(dat As Date) As Boolean
' this does not check for bank holidays
' may need adapting for non-English languages
' may need adapting for non-Gregorian calendars
Return ((dat.DayOfWeek = DayOfWeek.Saturday) Or (dat.DayOfWeek = DayOfWeek.Saturday))
End Function

 
Date.toString Parameters
Return dat.toString("____")
If the ____ string is exactly 1 character long, you may get unexpected results because There are special 1 character toString codes which produce pre-defined date and timme formats These formats tend to use American style date formats. When writing code for UK users, you should consider buiding your own date format using the codes below
 
All these examples assume tr>
d7
dd07
dddSun
ddddSunday
h1
12hr clock
hh01
12hr clock
H1
24hr clock
HH01
24hr clock
m2
mm02
M6
MM06
MMMJun
MMMMJune
s3
ss03
ttAM
yyyy2023
zzzTime Zone Offset
space
: or /
Spacer characters
Here is an example
Return dat.toString("dat1.ToString("hh:mm:ss tt '('zzz'),' ddd d MMM yyyy")")
Could return
"01:02:03 AM (+01:00), Thu 6 Jul 2023"
for an English computer using BST (British summer time = +01:00)
DigitalDan.co.uk ... Hits = 261