Visual Basic .NET - Date & Time Library Function

Now(): Returns the current date and time.

Dim currentDate As DateTime = Now()

Today(): Returns the current date.

Dim currentDate As Date = Today()

DateAdd(): Adds a specified time interval to a date.

Dim newDate As DateTime = DateAdd("d", 5, Now()) ' Adds 5 days to the current date

DateDiff(): Returns the difference between two dates.

Dim date1 As DateTime = #1/1/2022#
Dim date2 As DateTime = #1/15/2022#
Dim difference As TimeSpan = DateDiff(DateInterval.Day, date1, date2)

DatePart(): Returns a specific part of a date.

Dim year As Integer = DatePart(DateInterval.Year, Now())

DateSerial(): Creates a date from year, month, and day values.

Dim newDate As Date = DateSerial(2022, 5, 10)

DateValue(): Converts a string to a date.

Dim dateString As String = "5/10/2022"
Dim newDate As Date = DateValue(dateString)

Format(): Converts a date to a string using a specified format.

Dim currentDate As Date = Now()
Dim dateString As String = Format(currentDate, "MM/dd/yyyy")

IsDate(): Determines if a value is a valid date.

Dim dateString As String = "5/10/2022"
If IsDate(dateString) Then
    ' Do something
End If

Month(): Returns the month portion of a date.

Dim currentMonth As Integer = Month(Now())

Year(): Returns the year portion of a date.

Dim currentYear As Integer = Year(Now())

Weekday(): Returns the day of the week for a date.

Dim dayOfWeek As Integer = Weekday(Now())

WeekdayName(): Returns the name of the day of the week for a date.

Dim dayName As String = WeekdayName(Weekday(Now()))

MonthName(): Returns the name of the month for a date.

Dim monthName As String = MonthName(Month(Now()))

TimeOfDay(): Returns the current time.

Dim currentTime As DateTime = TimeOfDay()

Hour(): Returns the hour portion of a time.

Dim currentHour As Integer = Hour(Now())

Minute(): Returns the minute portion of a time.

Dim currentMinute As Integer = Minute(Now())

Second(): Returns the second portion of a time.

Dim currentSecond As Integer = Second(Now())

TimeSerial(): Creates a time from hour, minute, and second values.

Dim newTime As DateTime = TimeSerial(8, 30, 0) ' Creates a time of 8:30:00 AM

Timer(): Returns the number of seconds since midnight.

Dim secondsSinceMidnight As Double = Timer()