Date Values

The Date and DateTime data types are fundamentally the same thing. They share the same properties and methods. A Dateis a Visual Basic data type while DateTimeis the Common Language Runtime version of Date

A Date (e.g. May 22, 1982) can be defined in many ways in Visual Basic (a dozen according to Visual Studio IntelliSense)

Example 1:

Dim birthday As Date = #5/22/1982# ' Using enclosing #'s format, date must be M/D/YYYY form.

Example 2:

Dim birthday As Date = New Date(1982, 5, 22)

Example 3:

Dim birthday As Date = New Date(1982, 5, 22, 17, 45, 0) ' for 5:45 PM

A Date object can provide text information on its weekday and month name.

Examples:

birthday.DayOfWeek ' returns 6 (the sixth day of week)
birthday.DayOfWeek.ToString ' returns the text Saturday
birthday.ToLongDateString ' returns the text May-22-82
birthday.ToShortDateString ' returns the text 22/05/1982
birthday.DayOfYear ' returns integer 142, the day in the year
birthday.Hour ' returns 17
birthday.ToString("MMMM d") ' returns the text May 22
birthday.ToString("MM d, YYYY") ' returns the text May 22, 1982


The VB notation Date.Now returns the current date.

If you declare a Date variable but do not assign it a date, the default of January 1, 1 midnight is used.

Example

Dim firstDay As Date ' an unassigned date

MessageBox.Show(firstDay.toString) ' Displays 01/01/0001 12:00:00 AM

More on the MessageBox class.

The system date is the current date according to your computer's system clock. You can retrieve the system date using the syntax

Today[.methodname]

where methodname (optional) is ToLongDateString, ToShortDateString, or ToString( formatting characters)

The system timeis the current time according to your computer's system clock. You can retrieve the system time using the syntax

TimeOfDay[.methodname]

where methodname (optional) is ToLongTimeString, ToShortTimeString, or ToString( formatting characters)

Displaying the Date

To convert a Date to the format you need, use the Format function.

Example 1

Dim notice As String
notice = "The formatted date is " & Format(birthday, "dddd, d MMM yyyy")


displays the text "The formatted date is Saturday, 22 May 1982".

Finding the Time between two Dates

There are two ways to do this in VB.NET: the DateDiff function, or through the TimeSpan structure.

Both of these work equivalently but DateDiff is a function, so you need to call it every time you want to return a value. TimeSpan is a structure that is created once and then you just access its members. A TimeSpan object is not a Date object but represents an interval between two Dates.

With DateDiff, you call it with different date interval parameters.

DateDiff(DateInterval.Day, Date.Now, birthday) ' returns number of days since May 5, 1982
DateDiff(DateInterval.Hour, Date.Now, birthday) ' returns number of hours since May 5, 1982

The TimeSpan structure is created when you use the Date's Subtract

Dim diff As TimeSpan = birthday.Subtract( Date.Now )
diff.TotalDays ' is the number of days (will be a negative number)
diff.TotalHours ' the number of hours (will be a negative number)

If the Date argument in the Subtract method is after the first Date, the TimeSpan values will be negative.

Any Date can have time added to it with methods AddDays, AddHours, AddMinutes, etc.

birthday.AddDays( 365 ) ' date is a year later

A link to an MSDN article for some best practices coding with Date.

 

posted @ 2015-06-17 00:01  xymum  阅读(117)  评论(0编辑  收藏  举报