VB.Net - SPLessons

VB.Net Date and Time

Home > Lesson > Chapter 16
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Date and Time

VB.Net Date & Time

shape Description

Date & Time is a built in datatype of VB.Net. Sometimes a developer needs to display date and time for some applications. At that time date and time data type in VB.Net was very useful. Programmer can develop an application which is exactly matched with the real time watch and calendar by using VB.Net. The programmer can display only Date, only Time and also the combination of the both date and time. There is a class called DateAndTime Class which is used to do all the operations related to date and time. To do such operations, DateAndTime Class contains several Methods and Properties.

shape Properties

The following table shows you the Properties of the Date and Time.
Properties Description
Day Used to Get Day of the Month
Hour Used to Get Hour of the Date
Year Used to Get Year of the Date
Today Used to Get Current Date
Secound Used to Get Second of the Date
Mounth Used to Get Month of the Date
Now Used to Get Date and Time object Sets the Time and Date
Date Used to Get Date of this Instance
Minute Used to Get Minute of the Date
DayOfWeek Used to Gets DayofWeek
DayOf Year Used to Get DayofYear

shape Methods

The following table shows you the Methods of the Date and Time.
Methods Description
Add (Value As TimeSpan) As DateTime Used to Add the Specified TimeSpan to Current DateTime
AddHours (Value) As DateTime Used to Add the Specified Number of Hours to Current DateTime
AddMonths (Month) As DateTime Used to Add the Specified Number of Month to Current DateTime
AddYears (Value) As DateTime Used to Add the Specified Number of Years to Current DateTime
AddDays (Value) As DateTime Used to Add the Specified Number of Days to Current DateTime
AddMintues (Value) As DateTime Used to Add the Specified Number of Minutes to Current DateTime
AddSecounds (Value) As DateTime Used to Add the Specified Number of Seconds to Current DateTime
Compare (t1,t2 As Date Time) As Integer Used to Compare  DateTime and Returns DateTime
Equals (Value) As DateTime Used to Check Whether the two DataTimes are equal or not
CompareTo (Value) As DateTime Used to Compare Specified DateTime Values and Returns as Integer

shape Example

The following example code shows you "How to display the current date and time". [vbnet] Module Module1 Sub Main() Console.Write("Current Time: ") Console.WriteLine(Now.ToString()) 'Displays current time Dim dt As Date = Today 'Today is the properties which is used to get today's date Console.WriteLine("Current Date is: {0}", dt) 'Disaplys current date Console.ReadKey() End Sub End Module [/vbnet] Output:

shape Examples

The following example code shows you different methods of date and time. [vbnet] Module Module1 Sub Main() Dim customdate As Date = #8/12/2015 10:00:52 AM# Dim currenttime As Date = Date.Now Dim systemdate As Date = Date.UtcNow Dim currentdate As Date = Date.Today Console.WriteLine(customdate) Console.WriteLine(currentdate) Console.WriteLine(currentdate) Console.WriteLine(systemdate) Console.ReadKey() End Sub End Module [/vbnet] Output:

Summary

shape Points

  • Hash sign(#) is utilized to encase the information strict.
  • 'Today' is the property of the  DateandTime class.