VB.NET’s DateTime structure represents an
instant in time and is usually expressed as a particular date and time of the
day. DateTime
comes in handy whenever you need to determine the system’s date and time; it
also performs various operations on date/time variables. In this article, I
show how to utilize DateTime
properties and methods so you can work with DateTime
values in VB.NET.
Here are listings of the most commonly used properties and
methods of the DateTime
structure.
Properties
- Date: returns the date component
of the DateTime value. - Day: returns the day of the month component
of the DateTime value. - DayOfWeek: returns the day of the week component
of the DateTime value. - DayOfYear: returns the day of the year component
of the DateTime value. - Hour: returns the hour component
of the DateTime value. - Millisecond: returns the
milliseconds component of the DateTime value. - Minute: returns the minute
component of the DateTime value. - Month: returns the month component
of the DateTime value. - Now: returns a DateTime
value that is the current local date and time on this computer. - Second: returns the seconds
component of the DateTime value. - TimeOfDay: returns the time of day of the DateTime value.
- Today: returns the current system date.
- UtcNow: returns a DateTime
value that is the current local date and time on this computer expressed
as the Coordinated Universal Time (UTC). - Year: returns the year component
of the DateTime value.
Methods
- Add: adds the value of the
specified TimeSpan to the DateTime
value. - AddDays: adds the specified number of
days to the DateTime value. - AddHours: adds
the specified number of hours to the DateTime value. - AddMilliseconds: adds the specified number of
milliseconds to the DateTime value. - AddMinutes: adds the specified number of
minutes to the DateTime value. - AddMonths: adds the specified number of
months to the DateTime value. - AddSeconds:
adds the specified number of seconds to the DateTime
value. - AddYears: adds the specified number of
years to the DateTime value. - DaysInMonth: returns the number of days in
the specified month of the specified year. - IsLeapYear: returns an indication of whether
the specified year is a leap year. - Subtract: subtracts the specified
time or duration from the DateTime value. - ToLocalTime: converts the current Coordinated
Universal Time (UTC) to local time. - ToLongDateString: converts the value of this
instance to its equivalent long date string representation. - ToLongTimeString: converts the value of this
instance to its equivalent long time string representation. - ToShortDateString: converts the value of this
instance to its equivalent short date string representation. - ToShortTimeString: converts the value of this
instance to its equivalent short time string representation. - ToUniversalTime: converts the current local time
to Coordinated Universal Time (UTC).
An example
The following sample code demonstrates how to get the system’s
DateTime:
Dim Now AsDateTime = DateTime.Now
MessageBox.Show(Now)
We define a variable Now as a DateTime and set it to the property Now of the DateTime structure. The result of Now is the current system’s
DateTime value.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya’s column.
Advance your scripting skills to the next level with TechRepublic’s free Visual Basic newsletter, delivered each Friday. Automatically sign up today!