Javatpoint Logo
Javatpoint Logo

VB.NET Date & Time

In VB.NET, we use the Date and Time function to perform 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 edited, upgradation of new software version or patch-up details, etc.

In DateTime class, Date datatype stores date values, time values or date, and time values. Furthermore, to perform the date and time function, we need to import the System.DateTime class. The default value of DateTime is between 00:00:00 midnight, Jan 1, 0001 to 11:59:59 P.M., Dec 31, 9999 A.D.

Properties and method of DateTime

Date: It is used to return the date component of the DateTime Object.

Day: It is used to return the day of the month represented by the DateTime object.

DayOfWeek: It is used to return a particular day of the week represented by the DateTime object.

Minute: The Minute property is used to return the minute component by the DateTime object.

DateOfYear: It is used to return a day of the year represented by the DateTime object.

Hour: It is used to return the hour of the component of the date represented by the DateTime object.

Now: It is used to return the current date and time of the local system.

Month: The Month property is used to return the month name of the Datetime object.

Second: It is used to return the second of the DateTime object.

Ticks: It is used to return the number of ticks that refer to the DateTime object.

Today: It is used to return the current date of the system.

Year: It is used to return the year of the date represented by the DateTime object.

TimeOfDay: It is used to return the time of the day represented by the DateTime object.

Methods

The following are the most commonly used methods of the DateTime.

DaysInMonth: The DaysInMonth method is used to return the total number of days in the specified month of the year.

Add: It is used to return a new DateTime value by adding the timespan value to the DateTime object value.

AddHours: It is used to return a new time by adding the hours to the value of the Datetime object.

AddYears: It is used to return the year by adding the year to the value of the DateTime object.

AddDays: It is used to return the new Day by adding the days to the value of the DateTime object.

AddMinutes: It is used to display the new time by adding the minutes to the Datetime object.

AddMonths: It is used to return the new time by adding the months to the value of the Datetime object.

AddSeconds: It is used to return the new time by adding the seconds to the value of the Datetime object.

IsLeapYear: It uses a Boolean value that represents whether the particular year is a leap year or not.

Syntax:

Let's create an object of the DateTime.

Here, DateTime is a class for creating objects with the new keyword, and obj_name is the name of the object.

Let's create a program to show the different functions of DateTime Class in VB.NET.

DiffDateTime.vb

Output:

VB.NET Date & Time

Get the Current Date and Time

Let's create a program to get the current Date and Time in VB.NET.

CurrDateTime.vb

Output:

VB.NET Date & Time

Calculate DOB

To calculate DOB in a program, we need to use TimeSpan. The Timespan is used to store the difference between two dates. The difference between TimeSpan and DateTime is that the TimeSpan represents the time interval that can be a year or seconds, while DateTime represents the entire date over time.

In the following example, we will calculate the age by using the Date of Birth (DOB) of the user.

DofBirth.vb

Output:

VB.NET Date & Time

Formatting Date

As the name defines, a date format is used in any application or web application to represent the date in different formats. For example, we specified the Date literal as #5/5/2018# for the date May 5, 2018 that represents the mm/dd/yyyy format of the date. We can also use the format dd/mm/yyyy to display the date 12 August, 2019. If a locale user used another format like, yyyy/mm/dd, the literal would be invalid or compile error.

Here, we used the Format function of string class to convert a date literal to custom or local format. Further, we can also specify a predefined or user-defined format of the date.

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

Date_Format.vb

Output:

VB.NET Date & Time




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA