VB.NET DateTime

Array Collections File Keyword String Cast Class Console Data Dictionary Enum Exception If Interface Lambda LINQ List Loop Nothing Number Process Property Regex Select Sort Split StringBuilder Structure Sub Time Windows

Time

DateTime. This Structure represents a point in time.
It stores times,
like 12 AM,
and dates like yesterday. With TimeSpan we capture time differences.

TimeSpan
About part

Features. Methods are used to compute relative times such as yesterday and tomorrow. DateTime can be formatted—this changes it to a String.

Format
Constructor: create new

New instance. A DateTime instance can be created with the New operator. There are many overloads. But the overload shown here accepts three arguments.

Arguments:The arguments here are the year, the month, and the day of that month. The value 2 stands for February.

Warning:If you pass an invalid argument (one that no date could have), you will get an ArgumentOutOfRangeException.

Based on:

.NET 4.5

VB.NET program that uses constructor

Module Module1
    Sub Main()
	Dim value As DateTime = New DateTime(2014, 2, 1)

	' Display the date.
	Console.WriteLine(value)

	' Test the year.
	If value.Year = 2014 Then
	    Console.WriteLine("Year is 2014")
	End If
    End Sub
End Module

Output

2/1/2014 12:00:00 AM
Year is 2014
Programming tip

Yesterday. To get yesterday, we can access the Today property and then add negative one days to it. This is equivalent to subtracting one day from the DateTime instance.

VB.NET program that computes yesterday

Module Module1
    Sub Main()
	' Write the today value.
	Console.WriteLine("Today: {0}", DateTime.Today)
	' Subtract one day.
	Dim yesterday As DateTime = DateTime.Today.AddDays(-1)
	' Write the yesterday value.
	Console.WriteLine("Yesterday: {0}", yesterday)
    End Sub
End Module

Output

Today: 1/17/2013 12:00:00 AM
Yesterday: 1/16/2013 12:00:00 AM
Add

Tomorrow. This is a good day to schedule less pleasurable tasks. To compute tomorrow, we add one day with Add Days() on the value returned by the Today property.

Note:The output of the program will be different depending on when you run it.

VB.NET program that computes tomorrow

Module Module1
    Sub Main()
	' Write Today.
	Console.WriteLine("Today: {0}", DateTime.Today)
	' Add one to Today to get tomorrow.
	Dim tomorrow As DateTime = DateTime.Today.AddDays(1)
	' Write.
	Console.WriteLine("Tomorrow: {0}", tomorrow)
    End Sub
End Module

Output

Today: 1/17/2013 12:00:00 AM
Tomorrow: 1/18/2013 12:00:00 AM
First

First day. Getting the first day in a year is useful. We defines two new methods to get the first day in a year. When no argument is specified, the current year (for Today) is used.

VB.NET program that finds first day

Module Module1
    Sub Main()
	' Write first day of current year.
	Console.WriteLine("First day: {0}", FirstDayOfYear)
	' Write first day of 1999.
	Dim y As New DateTime(1999, 6, 1)
	Console.WriteLine("First day of 1999: {0}", FirstDayOfYear(y))
    End Sub

    ''' <summary>
    ''' Get first day of the current year.
    ''' </summary>
    Private Function FirstDayOfYear() As DateTime
	Return FirstDayOfYear(DateTime.Today)
    End Function

    ''' <summary>
    ''' Get first day of the specified year.
    ''' </summary>
    Private Function FirstDayOfYear(ByVal y As DateTime) As DateTime
	Return New DateTime(y.Year, 1, 1)
    End Function
End Module

Output

First day: 1/1/2010 12:00:00 AM
First day of 1999: 1/1/1999 12:00:00 AM
Copyright

Last day. Here we compute the last day in a year. This may help with year-based record keeping. The method gets the first day of the next year, and then subtracts one day.

Tip:Please see also the IsLeapYear Function, which can tell us whether an additional day is in the year or not.

VB.NET program that computes last day

Module Module1
    Sub Main()
	' Write current last day.
	Console.WriteLine("Last day: {0}", LastDayOfYear)
	' Write last day of 1999.
	Dim d As New DateTime(1999, 6, 1)
	Console.WriteLine("Last day of 1999: {0}", LastDayOfYear(d))
    End Sub

    ''' <summary>
    ''' Get last day of the current year.
    ''' </summary>
    Private Function LastDayOfYear() As DateTime
	Return LastDayOfYear(DateTime.Today)
    End Function

    ''' <summary>
    ''' Get last day of the specified year.
    ''' </summary>
    Private Function LastDayOfYear(ByVal d As DateTime) As DateTime
	Dim time As New DateTime((d.Year + 1), 1, 1)
	Return time.AddDays(-1)
    End Function
End Module

Output

Last day: 12/31/2010 12:00:00 AM
Last day of 1999: 12/31/1999 12:00:00 AM
Programming tip

DaysInMonth. Months contain different numbers of days. With the shared DaysInMonth Function, we get day-counts for a month in a specific year. The 2 means February.

VB.NET program that uses DaysInMonth

Module Module1
    Sub Main()
	' There are 28 days in February.
	Dim count As Integer = DateTime.DaysInMonth(2014, 2)
	Console.WriteLine(count)

	' Count days in March of 2014.
	Dim count2 As Integer = DateTime.DaysInMonth(2014, 3)
	Console.WriteLine(count2)
    End Sub
End Module

Output

28
31
String face

DateString. This property, only offered in VB.NET, returns the current date in String representation. Usually, DateTime-based methods are preferable—they are more standard.

Internals:DateString simply accesses the Today property on DateTime, and then it calls ToString with a format string.

Tip:Many global functions in the VB.NET language are implemented with the standard .NET libraries.

VB.NET that uses DateString

Module Module1
    Sub Main()
	Console.WriteLine(DateString)
    End Sub
End Module

Output

05-18-2011
Calendar: June 15

Date. This is another VB.NET special type. It aliases the DateTime type and has all the same methods. We can use Date interchangeably with DateTime.

Note:Usually DateTime is preferred in .NET programs as it is standard—all C# programs use DateTime not Date.

VB.NET that uses Date type

Module Module1
    Sub Main()
	' The Date type is the same as the DateTime type.
	Dim d As Date = New Date(2014, 10, 6)
	Dim d2 As DateTime = New DateTime(2014, 10, 6)

	If d = d2 Then
	    Console.WriteLine("Equal dates")
	End If
    End Sub
End Module

Output

Equal dates
The current time: now

Now property. This property is the same as Today, but it fills in the time. So it represents both the current time and the current date—Today just represents the current date.

DateTime.Now
String type

Parse. DateTime.Parse converts a string to a DateTime. If we read in string data from user input or a database, we can parse it into a DateTime for further processing.

DateTime.Parse
Squares: grey

Timing. Timers are excellent for checking up on your program with a diagnostic method. They can execute any code.
We can,
for example,
run a Timer every 30 seconds.

TimerStopwatchBenchmarks

A helpful type. We used the DateTime type in the VB.NET language in some specific, useful ways. The examples provide an overall glimpse into the DateTime type but not a comprehensive view.

VB.NET