Date Time in VB.net – How To Display Running Date and Time

What is Date and Time in VB.net?

The Date and Time In VB.net function performs various operations related to date and time.

Sometimes we need to display the date and time in our application or web application, such as when the last post was edited, upgrade of a new software version or patch-up details, etc.

Most of the software you write needs to implement some form of date function returning the 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 make 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.

Declaration of Date and Time in VB.net

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

Properties and Methods of the DateTime Structure in VB.net

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

#PropertyDescription
1.DateGets the date component of this instance.
2.DayGets the day of the month represented by this instance.
3.DayOfWeekGets the day of the week represented by this instance.
4.DayOfYearGets the day of the year represented by this instance.
5.HourGets the hour component of the date represented by this instance.
6.KindGets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
7.MillisecondGets the milliseconds component of the date represented by this instance.
8.MinuteGets the minute component of the date represented by this instance.
9.MonthGets the month component of the date represented by this instance.
10.NowGets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
11.SecondGets the seconds component of the date represented by this instance.
12.TicksGets the number of ticks that represent the date and time of this instance.
13.TimeOfDayGets the time of day for this instance.
14.TodayGets the current date.
15.UtcNowGets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
16.YearGets the year component of the date represented by this instance.
Properties and Methods of the DateTime Structure in VB.net

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

#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.

Creating a DateTime Object in VB.net

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.

Example program in Creating a DateTime Object in VB.net

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 AM
6/28/2022 7:13:57 AM
6/28/2022 7:13:57 AM
6/28/2022 12:00:00 AM

You can test the above example here! ➡ VB.net Online Compiler

Getting the Current Date and Time in VB.net

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: 8:19:59 AM

You can test the above example here! ➡ VB.net Online Compiler

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:

Current Date:
Today is: 6/28/2022 12:00:00 AM

You can test the above example here! ➡ VB.net Online Compiler

Formatting Date in VB.net

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

For example, you specified a Date literal of #2/6/2022# for the date February 6, 2022. 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, 2022. 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 the String class, specifying either a predefined or user-defined date format.

Example Program of Formatting Date in VB.net

Let’s create a program to display the various format of date in VB.NET.

Imports System.DateTime  
Module Date_Format  
    Sub Main()  
        Console.WriteLine(" Display the different Format of Dates in VB.NET ")  
        Dim FT As DateTime = New DateTime(2005, 7, 26, 0, 0, 0)  
        ' Use format Specifier to display the various format of the date.  
        Console.WriteLine(" Format of Date 'd' : " & FT.ToString("d"))  
        Console.WriteLine(" Format of Date 'D' : " & FT.ToString("D"))  
        Console.WriteLine(" Format of Date 'R' : " & FT.ToString("R"))  
        Console.WriteLine(" Format of Date 'y' : " & FT.ToString("y"))  
        Console.WriteLine(" Format of Date 'f' : " & FT.ToString("f"))  
        Console.WriteLine(" Format of Date 'F' : " & FT.ToString("F"))  
        Console.WriteLine(" Format of Date 't' : " & FT.ToString("t"))  
        Console.WriteLine(" Format of Date 'T' : " & FT.ToString("T"))  
        Console.WriteLine(" Format of Date 'g' : " & FT.ToString("g"))  
        Console.WriteLine(" Format of Date 'G' : " & FT.ToString("G"))  
        Console.WriteLine(" Format of Date 'M' : " & FT.ToString("M"))  

        Console.ReadKey()  
    End Sub  
End Module  

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

Display the different Format of Dates in VB.NET
Format of Date ‘d’ : 7/26/2022
Format of Date ‘D’ : Tuesday, July 26, 2022
Format of Date ‘R’ : Tue, 26 Jul 2022 00:00:00 GMT
Format of Date ‘y’ : July 2022
Format of Date ‘f’ : Tuesday, July 26, 2022 12:00 AM
Format of Date ‘F’ : Tuesday, July 26, 2022 12:00:00 AM
Format of Date ‘t’ : 12:00 AM
Format of Date ‘T’ : 12:00:00 AM
Format of Date ‘g’ : 7/26/2022 12:00 AM
Format of Date ‘G’ : 7/26/2022 12:00:00 AM
Format of Date ‘M’ : July 26

You can test the above example here! ➡ VB.net Online Compiler

Predefined Date/Time Formats in VB.net

The following table identifies the predefined date and time format names. These may be used by name as the style argument for the Format function.

FormatDescription
General Date, or GDisplays a date and/or time. For example, 1/12/2012 07:07:30 AM.
Long Date,Medium Date, or DDisplays a date according to your current culture’s long date format. For example, Sunday, December 16, 2012.
Short Date, or dDisplays a date using your current culture’s short date format. For example, 12/12/2012.
Long Time,Medium Time, orTDisplays a time using your current culture’s long time format; typically includes hours, minutes, seconds. For example, 01:07:30 AM.
Short Time or tDisplays a time using your current culture’s short time format. For example, 11:07 AM.
fDisplays the long date and short time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15 AM.
FDisplays the long date and long time according to your current culture’s format. For example, Sunday, December 16, 2012 12:15:31 AM.
gDisplays the short date and short time according to your current culture’s format. For example, 12/16/2012 12:15 AM.
M, mDisplays the month and the day of a date. For example, December 16.
R, rFormats the date according to the RFC1123Pattern property.
sFormats the date and time as a sortable index. For example, 2012-12-16T12:07:31.
uFormats the date and time as a GMT sortable index. For example, 2012-12-16 12:15:31Z.
UFormats the date and time with the long date and long time as GMT. For example, Sunday, December 16, 2012 6:07:31 PM.
Y, yFormats the date as the year and month. For example, December, 2012.
Predefined Date/Time Formats in VB.net

Properties and Methods of the DateAndTime Class in VB.net

The following table lists some of the commonly used properties of the DateAndTime Class in VB.net.

#Property & Description
1.Date
Returns or sets a String value representing the current date according to your system.
2.Now
Returns a Date value containing the current date and time according to your system.
3.TimeOfDay
Returns or sets a Date value containing the current time of day according to your system.
4.Timer
Returns a Double value representing the number of seconds elapsed since midnight.
5.TimeString
Returns or sets a String value representing the current time of day according to your system.
6.Today
Gets the current date.
Properties of the DateAndTime Class in VB.net

The following table lists some of the commonly used methods of the DateAndTime class in VB.net.

#Method Name & Description
1.Public Shared Function DateAdd (Interval As DateInterval, Number As Double, DateValue As DateTime) As DateTime
Returns a Date value containing a date and time value to which a specified time interval has been added.
2.Public Shared Function DateAdd (Interval As String,Number As Double,DateValue As Object ) As DateTime
Returns a Date value containing a date and time value to which a specified time interval has been added.
3.Public Shared Function DateDiff (Interval As DateInterval, Date1 As DateTime, Date2 As DateTime, DayOfWeek As FirstDayOfWeek, WeekOfYear As FirstWeekOfYear ) As Long
Returns a Long value specifying the number of time intervals between two Date values.
4.Public Shared Function DatePart (Interval As DateInterval, DateValue As DateTime, FirstDayOfWeekValue As FirstDayOfWeek, FirstWeekOfYearValue As FirstWeekOfYear ) As Integer
Returns an Integer value containing the specified component of a given Date value.
5.Public Shared Function Day (DateValue As DateTime) As Integer
Returns an Integer value from 1 through 31 representing the day of the month.
6.Public Shared Function Hour (TimeValue As DateTime) As Integer
Returns an Integer value from 0 through 23 representing the hour of the day.
7.Public Shared Function Minute (TimeValue As DateTime) As Integer
Returns an Integer value from 0 through 59 representing the minute of the hour.
8.Public Shared Function Month (DateValue As DateTime) As Integer
Returns an Integer value from 1 through 12 representing the month of the year.
9.Public Shared Function MonthName (Month As Integer, Abbreviate As Boolean) As String
Returns a String value containing the name of the specified month.
10.Public Shared Function Second (TimeValue As DateTime) As Integer
Returns an Integer value from 0 through 59 representing the second of the minute.
11.Public Overridable Function ToString As String
Returns a string that represents the current object.
12.Public Shared Function Weekday (DateValue As DateTime, DayOfWeek As FirstDayOfWeek) As Integer
Returns an Integer value containing a number representing the day of the week.
13.Public Shared Function WeekdayName (Weekday As Integer, Abbreviate As Boolean, FirstDayOfWeekValue As FirstDayOfWeek) As String
Returns a String value containing the name of the specified weekday.
14.Public Shared Function Year (DateValue As DateTime) As Integer
Returns an Integer value from 1 through 9999 representing the year.
Methods of the DateAndTime class in VB.net.

Example Program of Methods of the DateAndTime class in VB.net.

The following program demonstrates some of these methods.

Module Module1
   Sub Main()
      Dim birthday As Date
      Dim bday As Integer
      Dim month As Integer
      Dim monthname As String
      ' Assign a date using standard short format.
      birthday = #2/11/1996#
      bday = Microsoft.VisualBasic.DateAndTime.Day(birthday)
      month = Microsoft.VisualBasic.DateAndTime.Month(birthday)
      monthname = Microsoft.VisualBasic.DateAndTime.MonthName(month)
      
      Console.WriteLine(birthday)
      Console.WriteLine(bday)
      Console.WriteLine(month)
      Console.WriteLine(monthname)
      Console.ReadKey()
   End Sub
End Module

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

2/11/1996 12:00:00 AM
11
2
February

You can test the above example here! ➡ VB.net Online Compiler

Summary

In this tutorial, we’ve discussed the Date and Time in VB.net and how to declare Date and Time using Visual Basic Programming, also we give examples for the different format of Data and Time in VB.net.


Leave a Comment