On CBS MoneyWatch: 6 ways to get cash for college

Working with dates and times in VB.NET

Tags: Microsoft development tools, Irina Medvinskaya, Microsoft Visual Basic.Net, DateTime, DateTime value, Visual Basic Tips Newsletter

  • Save
  • Print
  • Recommend
  • 30

Takeaway: VB.NET's DateTime structure represents an instant in time. Irina Medvinskaya discusses how to utilize DateTime properties and methods so you can work with DateTime values in VB.NET.

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!

  • Save
  • Print
  • Recommend
  • 30

Print/View all Posts Comments on this article

wish it was more informative arzewski@... | 06/30/06
karthika karthiprakash07@... | 04/18/08
No much info spam@... | 10/29/06
RE: Working with dates and times in VB.NET ravi_maduri_mca@... | 08/16/07
Hi I got The Soltion for above dates ravi_maduri_mca@... | 08/17/07
The all powerful Format function. sales@... | 08/21/07
RE: Working with dates and times in VB.NET karthiprakash07@... | 04/18/08
RE: Working with dates and times in VB.NET sheik_kpm@... | 07/17/08

What do you think?

White Papers, Webcasts, and Downloads

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

Introducing SmartPlanet

advertisement
Click Here