Most of the softwares you write need implementing some form of date functions returning current date and time. Dates are so much part of everyday life that it becomes easy to work with them without thinking. VB.Net also provides powerful tools for date arithmetic that makes manipulating dates easy.

The Date data type contains date values, time values, or date and time values. The default value of Date is 0:00:00 (midnight) on January 1, 0001. The equivalent .NET data type is System.DateTime.

The DateTime structure represents an instant in time, typically expressed as a date and time of day

'Declaration
<SerializableAttribute> _
Public Structure DateTime _
	Implements IComparable, IFormattable, IConvertible, ISerializable,  
	IComparable(Of DateTime), IEquatable(Of DateTime)

You can also get the current date and time from the DateAndTime class.

The DateAndTime module contains the procedures and properties used in date and time operations.

'Declaration
<StandardModuleAttribute> _
Public NotInheritable Class DateAndTime
Note:

Both the DateTime structure and the DateAndTime module contain properties like Now and Today, so often beginners find it confusing. The DateAndTime class belongs to the Microsoft.VisualBasic namespace and the DateTime structure belongs to the System namespace.
Therefore, using the later would help you in porting your code to another .Net language like C#. However, the DateAndTime class/module contains all the legacy date functions available in Visual Basic.

Properties and Methods of the DateTime Structure

The following table lists some of the commonly used properties of the DateTime Structure:

S.N Property Description
1 Date Gets the date component of this instance.
2 Day Gets the day of the month represented by this instance.
3 DayOfWeek Gets the day of the week represented by this instance.
4 DayOfYear Gets the day of the year represented by this instance.
5 Hour Gets the hour component of the date represented by this instance.
6 Kind Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
7 Millisecond Gets the milliseconds component of the date represented by this instance.
8 Minute Gets the minute component of the date represented by this instance.
9 Month Gets the month component of the date represented by this instance.
10 Now Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
11 Second Gets the seconds component of the date represented by this instance.
12 Ticks Gets the number of ticks that represent the date and time of this instance.
13 TimeOfDay Gets the time of day for this instance.
14 Today Gets the current date.
15 UtcNow Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
16 Year Gets the year component of the date represented by this instance.

The following table lists some of the commonly used methods of the DateTime structure:

S.N Method Name & Description
1 Public Function Add ( value As TimeSpan ) As DateTime 
Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.
2 Public Function AddDays ( value As Double ) As DateTime 
Returns a new DateTime that adds the specified number of days to the value of this instance.
3 Public Function AddHours ( value As Double ) As DateTime 
Returns a new DateTime that adds the specified number of hours to the value of this instance.
4 Public Function AddMinutes ( value As Double ) As DateTime 
Returns a new DateTime that adds the specified number of minutes to the value of this instance.
5 Public Function AddMonths ( months As Integer ) As DateTime 
Returns a new DateTime that adds the specified number of months to the value of this instance.
6 Public Function AddSeconds ( value As Double ) As DateTime 
Returns a new DateTime that adds the specified number of seconds to the value of this instance.
7 Public Function AddYears ( value As Integer ) As DateTime 
Returns a new DateTime that adds the specified number of years to the value of this instance.
8 Public Shared Function Compare ( t1 As DateTime, t2 As DateTime ) As Integer 
Compares two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance.
9 Public Function CompareTo ( value As DateTime ) As Integer 
Compares the value of this instance to a specified DateTime value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateTime value.
10 Public Function Equals ( value As DateTime ) As Boolean 
Returns a value indicating whether the value of this instance is equal to the value of the specified DateTime instance.
11 Public Shared Function Equals ( t1 As DateTime, t2 As DateTime ) As Boolean 
Returns a value indicating whether two DateTime instances have the same date and time value.
12 Public Overrides Function ToString As String 
Converts the value of the current DateTime object to its equivalent string representation.

The above list of methods is not exhaustive, please visit Microsoft documentation for the complete list of methods and properties of the DateTime structure.

Creating a DateTime Object

You can create a DateTime object in one of the following ways:

  • By calling a DateTime constructor from any of the overloaded DateTime constructors.

  • By assigning the DateTime object a date and time value returned by a property or method.

  • By parsing the string representation of a date and time value.

  • By calling the DateTime structure's implicit default constructor.

The following example demonstrates this:

Module Module1
   Sub Main()
      'DateTime constructor: parameters year, month, day, hour, min, sec
      Dim date1 As New Date(2012, 12, 16, 12, 0, 0)
      'initializes a new DateTime value
      Dim date2 As Date = #12/16/2012 12:00:52 AM#
      'using properties
      Dim date3 As Date = Date.Now
      Dim date4 As Date = Date.UtcNow
      Dim date5 As Date = Date.Today
      Console.WriteLine(date1)
      Console.WriteLine(date2)
      Console.WriteLine(date3)
      Console.WriteLine(date4)
      Console.WriteLine(date5)
      Console.ReadKey()
   End Sub
End Module

When the above code was compiled and executed, it produces the following result:

12/16/2012 12:00:00 PM
12/16/2012 12:00:52 PM
12/12/2012 10:22:50 PM
12/12/2012 12:00:00 PM

Getting the Current Date and Time:

The following programs demonstrate how to get the current date and time in VB.Net:

Current Time:

Module dateNtime
   Sub Main()
      Console.Write("Current Time: ")
      Console.WriteLine(Now.ToLongTimeString)
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result:

Current Time: 11 :05 :32 AM

Current Date:

Module dateNtime
   Sub Main()
      Console.WriteLine("Current Date: ")
      Dim dt As Date = Today
      Console.WriteLine("Today is: {0}", dt)
      Console.ReadKey()
   End Sub
End Module

When the above code is compiled and executed, it produces the following result:

Today is: 12/11/2012 12:00:00 AM

Formatting Date

A Date literal should be enclosed within hash signs (# #), and specified in the format M/d/yyyy, for example #12/16/2012#. Otherwise, your code may change depending on the locale in which your application is running.

For example, you specified Date literal of #2/6/2012# for the date February 6, 2012. It is alright for the locale that uses mm/dd/yyyy format. However, in a locale that uses dd/mm/yyyy format, your literal would compile to June 2, 2012. If a locale uses another format say, yyyy/mm/dd, the literal would be invalid and cause a compiler error.

To convert a Date literal to the format of your locale or to a custom format, use the Format function of String class, specifying either a predefined or user-defined date format.

The following example demonstrates this.

Module dateNtime
   Sub Main()
      Console.WriteLine("India Wins Freedom: ")
      Dim independenceDay As New Date(1947, 8, 15,<