Home
VB.NET
DateTime Examples
This page was last reviewed on Feb 1, 2024.
Dot Net Perls
DateTime. This VB.NET Structure represents a point in time. It stores times, like 12 AM, and dates like yesterday. With TimeSpan we capture time differences.
TimeSpan
Methods are used to compute relative times such as yesterday and tomorrow. DateTime can be formatted—this changes it to a String.
DateTime Format
New instance. A DateTime instance can be created with the New operator. There are many overloads. But the overload shown here accepts 3 arguments.
Part 1 The arguments here are the year, the month, and the day of that month. The value 2 stands for February.
Part 2 Here we access the Year property on the DateTime instance we just created.
Warning If you pass an invalid argument (one that no date could have), you will get an ArgumentOutOfRangeException.
Module Module1 Sub Main() ' Part 1: create new instance. Dim value As DateTime = New DateTime(2022, 2, 1) Console.WriteLine(value) ' Part 2: test the year. If value.Year = 2022 Then Console.WriteLine("Year is 2022") End If End Sub End Module
2/1/2022 12:00:00 AM Year is 2022
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.
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
Today: 1/17/2010 12:00:00 AM Yesterday: 1/16/2010 12:00:00 AM
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.
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
Today: 1/17/2010 12:00:00 AM Tomorrow: 1/18/2010 12:00:00 AM
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.
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
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 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.
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
Last day: 12/31/2010 12:00:00 AM Last day of 1999: 12/31/1999 12:00:00 AM
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.
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
28 31
Get DayOfWeek. Every day in the future of our universe will have a day of week—like Monday or Tuesday. If you have a special day you need to test for, use the DayOfWeek property.
Info This property returns a DayOfWeek Enum value. We can test it against a DayOfWeek enumerated constant.
Module Module1 Sub Main() ' Get current day of the week. Dim day As DayOfWeek = DateTime.Today.DayOfWeek ' Write the current day. Console.WriteLine("TODAY IS {0}", day) End Sub End Module
TODAY IS Wednesday
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.
Detail 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.
Module Module1 Sub Main() Console.WriteLine(DateString) End Sub End Module
05-18-2011
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.
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
Equal dates
DateTime.MinValue. Suppose we have a DateTime that we are not ready to assign to an actual value. We can use a special constant like MinValue to mean the default, zero value.
Also We can use a nullable DateTime to mean "no value is set." A nullable may be clearer for this use in programs.
Module Module1 Sub Main() Dim value As DateTime = DateTime.MinValue Console.WriteLine("MINVALUE: {0}", value) ' Test for MinValue. If value = DateTime.MinValue Then Console.WriteLine("MIN VALUE IS SET") End If End Sub End Module
MINVALUE: 1/1/0001 12:00:00 AM MIN VALUE IS SET
Error, un-representable. We cannot create a DateTime that cannot possibly exist. Here we try to use a month of 20 in the constructor, but get an "un-representable" error.
Module Module1 Sub Main() ' Cannot have a month of 20, no such month exists. Dim value As DateTime = New DateTime(2000, 20, 1) End Sub End Module
Unhandled Exception: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime. at System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day) at System.DateTime..ctor(Int32 year, Int32 month, Int32 day)...
Nothing. We cannot assign a DateTime to Nothing—we must use a special value like MinValue to mean "no date." But we can also use a Nullable DateTime.
Info A Nullable type is a structure that wraps a value type like the DateTime. We can set a nullable DateTime to Nothing.
Here In this program we set our nullable DateTime to nothing. And the IsNothing function recognizes our value is nothing.
Nullable
Module Module1 Sub Main() ' Use nullable DateTime. Dim test As DateTime? = New DateTime(2000, 1, 1) Console.WriteLine("GETVALUEORDEFAULT: {0}", test.GetValueOrDefault()) ' The nullable DateTime can be Nothing. test = Nothing Console.WriteLine("ISNOTHING: {0}", IsNothing(test)) End Sub End Module
GETVALUEORDEFAULT: 1/1/2000 12:00:00 AM ISNOTHING: True
Summary. We used the DateTime type in the VB.NET language in some specific, useful ways. The examples provide a glimpse into the DateTime type but not a comprehensive view.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 1, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.