VB.NET's DateTime structure represents an instant in time which is commonly expressed as a particular date and time of the day. It's a part of your daily life while creating your applications which makes it easy for you to manipulate system's date and time. In this article I'll be showing you the commonly used
properties and
methods and example
usage of the Date and Time in VB.Net.
Use the Date data type to contain date values, time values, or date and time values.
The default value of Date is 0:00:00 (midnight) on January 1, 0001.
You can get the current date and time from the DateAndTime class.
DateTime Properties
The table below shows some of the commonly used properties of the DateTime Structure:
Property | Description |
Date | Gets the date component of this instance. |
Day | Gets the day of the month represented by this instance. |
DayOfWeek | Gets the day of the week represented by this instance. |
DayOfYear | Gets the day of the year represented by this instance. |
Hour | Gets the hour component of the date represented by this instance. |
Kind | Gets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither. |
Millisecond | Gets the milliseconds component of the date represented by this instance. |
Minute | Gets the minute component of the date represented by this instance. |
Month | Gets the month component of the date represented by this instance. |
Now | Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. |
Second | Gets the seconds component of the date represented by this instance. |
Ticks | Gets the number of ticks that represent the date and time of this instance. |
TimeOfDay | Gets the time of day for this instance. |
Today | Gets the current date. |
UtcNow | Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC). |
Year | Gets the year component of the date represented by this instance. |
DateTime Methods
The table below shows the commonly used Methods of the DateTime Structure.
Method Name | Description |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
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. |
Public Overrides Function ToString As String |
Converts the value of the current DateTime object to its equivalent string representation. |
DateTime Methods Example
The following snippet shows some of the DateTime Methods used in the DateTime Structure.
Public Sub DateTimeMethods()
Debug.Print("Today is: " & Now)
Debug.Print("Adds 1 Day to Current date: " & Now.AddDays(1))
Debug.Print("Adds 1 Hour to Current Time: " & Now.AddHours(1))
Debug.Print("Adds 1 Minute to Current Time: " & Now.AddMinutes(1))
Debug.Print("Adds 1 Second to Current Time: " & Now.AddSeconds(1))
End Sub
Output
The above snippet should print the following.
Today is: 9/6/2014 11:54:31 AM
Adds 1 Day to Current date: 9/7/2014 11:54:31 AM
Adds 1 Hour to Current Time: 9/6/2014 12:54:31 PM
Adds 1 Minute to Current Time: 9/6/2014 11:55:31 AM
Adds 1 Second to Current Time: 9/6/2014 11:54:32 AM
Formatting Date and Time
See formatting Date and Time tutorial here...
Comments
Post a Comment