VB.NET DateTime

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

DateTime

DateTime represents a point in time. It is a structure in the VB.NET language. It has many helpful methods. These are used to compute relative times such as yesterday and tomorrow. We can store important days.

Constructor

A DateTime instance can be created with the New operator. There are many overloads. But the overload shown here accepts three arguments: the year, the month, and the day of that month. The value 2 stands for February.

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

Based on:

.NET 4.5

Program that uses constructor: VB.NET

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

Yesterday

Programming tip

You may need to get a DateTime instance for yesterday. To do this, you can access the Today parameterless property and then add negative one days to it. This is equivalent to subtracting one day from the DateTime instance.

Program that computes yesterday value: VB.NET

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/2010 12:00:00 AM
Yesterday: 1/16/2010 12:00:00 AM

Tomorrow

Plus

Tomorrow is a useful day—it is a good day to schedule your less pleasurable tasks. To compute tomorrow, we add a positive one number of days using the AddDays instance method on the value returned by the Today property.

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

Program that computes tomorrow value: VB.NET

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/2010 12:00:00 AM
Tomorrow: 1/18/2010 12:00:00 AM

First day

One: 1

For programs that use calendar or year-based methods, getting the first day in a year is useful. This program text defines two new methods, each named FirstDayOfYear, which provide a way to get the first day in a specific year.

Program that finds first day of year: VB.NET

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

Last day

Here we compute the last day in the current year or in a specific year. This is useful also for programs that have some requirement for year-based record keeping. The method gets the first day of the next year, and then subtracts one day.

Program that computes last days: VB.NET

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

Type

Squares

The DateTime type has additional functionality. It can be formatted in certain ways for output. Also, properties such as Now are available—these return DateTime instances. DateTime.Parse converts a string to a DateTime.

DateTime FormatDateTime.NowDateTime.Parse

Also:The .NET Framework provides TimeSpan, which represents a period of time. The VB.NET language further offers DateString.

TimeSpanDateString

Timing. You can also use timers in your VB.NET programs. Timers are excellent for checking up on your program with a diagnostic method. They can execute any code. You can run a Timer every 30 seconds.

TimerStopwatchBenchmarks

Summary

The VB.NET programming language

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. But they are useful in certain program contexts.

VB.NET