VB.Net Tutorial/Date Time/Date Functions — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:54, 26 мая 2010
Содержание
- 1 Assign Now() to a date variable
- 2 Call to WeekdayName combined with call to Weekday
- 3 CDate: convert string(15 July, o Date
- 4 CDate: convert string( to date
- 5 CDate: convert string(Tuesday, September 6:45pm) to Date
- 6 DateAdd: add two date together
- 7 Determining the weekday of a date
- 8 Different date between two dates
- 9 TimeOfDay
- 10 Weekday Function
Assign Now() to a date variable
<source lang="vbnet">public class Test
public Shared Sub Main
Dim dteExpiration As Date dteExpiration = Now() Console.WriteLine(dteExpiration)
End Sub
End class</source>
11/05/2007 12:30:59 PM
Call to WeekdayName combined with call to Weekday
<source lang="vbnet">Option Strict On Imports System.Globalization Public Module WeekdayNameFunction
Public Sub Main() Dim birthday As Date = #07/28/1993# Console.WriteLine(WeekdayName(Weekday(birthday), True)) End Sub
End Module</source>
Wed
CDate: convert string(15 July, o Date
<source lang="vbnet">Module Tester
Public Sub Main() Dim sDate As String = "8/1/86" Dim dtDate As Date = CDate(sDate) sDate = "15 July, 1215" dtDate = CDate(sDate) Console.WriteLine("Date now holds {0}", dtDate) End Sub
End Module</source>
Date now holds 15/07/1215 12:00:00 AM
CDate: convert string( to date
<source lang="vbnet">Module Tester
Public Sub Main() Dim sDate As String = "8/1/86" Dim dtDate As Date = CDate(sDate) Console.WriteLine("Date now holds {0}", dtDate) End Sub
End Module</source>
Date now holds 08/01/1986 12:00:00 AM
CDate: convert string(Tuesday, September 6:45pm) to Date
<source lang="vbnet">Module Tester
Public Sub Main() Dim sDate As String = "8/1/86" Dim dtDate As Date = CDate(sDate) sDate = "Tuesday, September 16, 1845 6:45pm" dtDate = CDate(sDate) Console.WriteLine("Date now holds {0}", dtDate) End Sub
End Module</source>
Date now holds 16/09/1845 6:45:00 PM
DateAdd: add two date together
<source lang="vbnet">Option Strict On Public Module DateAddTest
Public Sub Main() Console.WriteLine(DateAdd(DateInterval.Hour, 36, Date.Now)) Console.WriteLine(DateAdd(DateInterval.Hour, -36, Date.Now)) End Sub
End Module</source>
13/05/2007 9:17:01 AM 10/05/2007 9:17:01 AM
Determining the weekday of a date
<source lang="vbnet">Option Strict On Imports System.Globalization Public Module WeekdayNameFunction
Public Sub Main() " Dim historicalDate As New DateTime(1905, 1, 9, New JulianCalendar()) Dim dayOfWeek As Integer = Weekday(historicalDate, FirstDayOfWeek.System) Console.WriteLine(WeekdayName(dayOfWeek, False, FirstDayOfWeek.System)) End Sub
End Module</source>
Sunday
Different date between two dates
<source lang="vbnet">public class Test
public Shared Sub Main
Dim dteExpiration As Date Dim lngDays As Long lngDays = DateDiff(DateInterval.Day, #12/31/2000#, Now()) Console.WriteLine(lngDays)
End Sub
End class</source>
2322
TimeOfDay
<source lang="vbnet">public class Test
public Shared Sub Main Console.WriteLine(TimeOfDay) End Sub
End Class</source>
01/01/0001 12:22:14 PM
Weekday Function
<source lang="vbnet">Option Strict On Public Module WeekdayFunction
Public Sub Main Dim holiday As Date = #01/01/2006# If Weekday(holiday, FirstDayOfWeek.Monday) <= 5 Then Console.WriteLine("The holiday on {0}, is during the work week.", _ FormatDateTime(holiday, DateFormat.LongDate)) Else Console.WriteLine("The holiday on {0}, occurs during the weekend.", _ FormatDateTime(holiday, DateFormat.LongDate)) End If End Sub
End Module</source>
The holiday on January 1, 2006, occurs during the weekend.