Planet For Application Life Development Presents
MY IT World

Explore and uptodate your technology skills...

VB.Net - Date & Time

Most of the software you write needs implementing some form of date functions returning current date and time. Dates are so much part of everyday life that it becomes easy to work with them without thinking. VB.Net also provides powerful tools for date arithmetic that makes manipulating dates easy.

The Date data type contains date values, time values, or date and time values. The default value of Date is 0:00:00 (midnight) on January 1, 0001. The equivalent .NET data type is System.DateTime.

The DateTime structure represents an instant in time, typically expressed as a date and time of day

'Declaration
<SerializableAttribute> _
Public Structure DateTime _
	Implements IComparable, IFormattable, IConvertible, ISerializable,  
	IComparable(Of DateTime), IEquatable(Of DateTime)

You can also get the current date and time from the DateAndTime class.

The DateAndTime module contains the procedures and properties used in date and time operations.

'Declaration
<StandardModuleAttribute> _
Public NotInheritable Class DateAndTime

Properties and Methods of the DateTime Structure

The following table lists some of the commonly used properties of the DateTime Structure:

S.NPropertyDescription
1DateGets the date component of this instance.
2DayGets the day of the month represented by this instance.
3DayOfWeekGets the day of the week represented by this instance.
4DayOfYearGets the day of the year represented by this instance.
5HourGets the hour component of the date represented by this instance.
6KindGets a value that indicates whether the time represented by this instance is based on local time, Coordinated Universal Time (UTC), or neither.
7MillisecondGets the milliseconds component of the date represented by this instance.
8MinuteGets the minute component of the date represented by this instance.
9MonthGets the month component of the date represented by this instance.
10NowGets a DateTime object that is set to the current date and time on this computer, expressed as the local time.
11SecondGets the seconds component of the date represented by this instance.
12TicksGets the number of ticks that represent the date and time of this instance.
13TimeOfDayGets the time of day for this instance.
14TodayGets the current date.
15UtcNowGets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).
16YearGets the year component of the date represented by this instance.

The following table lists some of the commonly used methods of the DateTime structure:

S.NMethod Name & Description
1Public 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.
2Public Function AddDays ( value As Double ) As DateTime
Returns a new DateTime that adds the specified number of days to the value of this instance.
3Public Function AddHours ( value As Double ) As DateTime
Returns a new DateTime that adds the specified number of hours to the value of this instance.
4Public Function AddMinutes ( value As Double ) As DateTime
Returns a new DateTime that adds the specified number of minutes to the value of this instance.
5Public Function AddMonths ( months As Integer ) As DateTime
Returns a new DateTime that adds the specified number of months to the value of this instance.
6Public Function AddSeconds ( value As Double ) As DateTime
Returns a new DateTime that adds the specified number of seconds to the value of this instance.
7Public Function AddYears ( value As Integer ) As DateTime
Returns a new DateTime that adds the specified number of years to the value of this instance.
8Public 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.
9Public 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.
10Public 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.
11Public 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.
12Public Overrides Function ToString As String
Converts the value of the current DateTime object to its equivalent string representation.

Creating a DateTime Object

You can create a DateTime object, in one of the following ways:

  • By calling a DateTime constructor from any of the overloaded DateTime constructors.

  • By assigning the DateTime object a date and time value returned by a property or method.

  • By parsing the string representation of a date and time value.

  • By calling the DateTime structure's implicit default constructor.

Predefined Date/Time Formats

The following table identifies the predefined date and time format names. These may be used by name as the style argument for the Format function:

FormatDescription
General Date, or GDisplays a date and/or time. For example, 1/12/2012 07:07:30 AM.
Long Date,Medium Date, or DDisplays a date according to your current culture's long date format. For example, Sunday, December 16, 2012.
Short Date, or dDisplays a date using your current culture's short date format. For example, 12/12/2012.
Long Time,Medium Time, orTDisplays a time using your current culture's long time format; typically includes hours, minutes, seconds. For example, 01:07:30 AM.
Short Time or tDisplays a time using your current culture's short time format. For example, 11:07 AM.
fDisplays the long date and short time according to your current culture's format. For example, Sunday, December 16, 2012 12:15 AM.
FDisplays the long date and long time according to your current culture's format. For example, Sunday, December 16, 2012 12:15:31 AM.
gDisplays the short date and short time according to your current culture's format. For example, 12/16/2012 12:15 AM.
M, mDisplays the month and the day of a date. For example, December 16.
R, rFormats the date according to the RFC1123Pattern property.
sFormats the date and time as a sortable index. For example, 2012-12-16T12:07:31.
uFormats the date and time as a GMT sortable index. For example, 2012-12-16 12:15:31Z.
UFormats the date and time with the long date and long time as GMT. For example, Sunday, December 16, 2012 6:07:31 PM.
Y, yFormats the date as the year and month. For example, December, 2012.

Properties and Methods of the DateAndTime Class

The following table lists some of the commonly used properties of the DateAndTime Class:

S.NPropertyDescription
1DateReturns or sets a String value representing the current date according to your system.
2NowReturns a Date value containing the current date and time according to your system.
3TimeOfDayReturns or sets a Date value containing the current time of day according to your system.
4TimerReturns a Double value representing the number of seconds elapsed since midnight.
5TimeStringReturns or sets a String value representing the current time of day according to your system.
6TodayGets the current date.