VB.Net/Development/Date Operation
Содержание
Add a day, a month and a year to a Date
Imports System
Public Class MainClass
Shared Sub Main()
Dim dteStartDate As Date
Dim dteChangedDate As Date
"Start off in 2400
dteStartDate = #2/28/2400#
"Add a day and display the results
dteChangedDate = dteStartDate.AddDays(1)
System.Console.WriteLine(dteChangedDate.ToLongDateString)
"Add some months and display the results
dteChangedDate = dteStartDate.AddMonths(6)
System.Console.WriteLine(dteChangedDate.ToLongDateString)
"Subtract a year and display the results
dteChangedDate = dteStartDate.AddYears(-1)
System.Console.WriteLine(dteChangedDate.ToLongDateString)
End Sub
End Class
Add time (days, hours) to current time
Imports System.IO
Module Module1
Sub Main()
Dim Dt As DateTime = Now()
Dim NewDT As DateTime = Now()
Console.WriteLine("Current date and time: " & Dt)
NewDT = NewDT.AddHours(5)
Console.WriteLine("Five hours from now is: " & NewDT)
End Sub
End Module
Date Time and TimeSpan Demo
Imports System
Imports System.Diagnostics
Public Class MainClass
Shared Sub Main()
Const TRIALS As Integer = 10000000
Dim start_time As DateTime
Dim stop_time As DateTime
Dim elapsed_time As TimeSpan
Dim i As Integer
start_time = Now
For i = 1 To TRIALS
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))
start_time = Now
For i = 1 To TRIALS
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))
End Sub
End Class
Date Time Subtraction
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim date1 As Date = #7/20/2004#
Dim date2 As Date = #8/7/2004#
Dim elapsed_time As TimeSpan
elapsed_time = Date.op_Subtraction(date2, date1)
Console.WriteLine(elapsed_time.Days.ToString)
End Sub
End Class
Multiply Time Span
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim ts1, ts2 As TimeSpan
ts1 = New TimeSpan(1, 2, 3)
ts2 = New TimeSpan(ts1.Ticks * 12)
Console.WriteLine(ts1.ToString)
Console.WriteLine(ts2.ToString)
End Sub
End Class