VB.Net Tutorial/Date Time/TimeSpan

Материал из VB Эксперт
Перейти к: навигация, поиск

Add TimeSpan to Date

Option Strict On
Public Class DateMembers
   Public Shared Sub Main()
      Dim dat As Date = New Date(2000, 1, 1)
      Dim ts As New TimeSpan(30, 0, 0, 0)
      Console.WriteLine("The original date is " & dat)
      dat = dat.AddMonths(5)
      Console.WriteLine("The new date is " & dat)
      dat = dat + ts
      Console.WriteLine("The new date is " & dat)
   End Sub
End Class
The original date is 01/01/2000
The new date is 01/06/2000
The new date is 01/07/2000

Date different parts

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim yearNow As Integer = rightNow.Year
        Dim monthNow As Integer = rightNow.Month
        Dim dayNow As Integer = rightNow.Day
        Dim results As String = String.Format( _
           "Year: {1}{0}Month: {2}{0}Day: {3}{0}", _
           vbNewLine, yearNow, monthNow, dayNow)
    End Sub
End Class
Year: 2007
Month: 5
Day: 11

Day of the week

Public Class Tester
    Public Shared Sub Main
    End Sub
End Class

Ticks times value

public class Test
   public Shared Sub Main
        Dim ts1, ts2 As TimeSpan
        ts1 = New TimeSpan(1, 2, 3)
        ts2 = New TimeSpan(ts1.Ticks * 12)
   End Sub
End class

TimeSpan and Date Substract

Public Class Tester
    Public Shared Sub Main
        Dim birthDay As Date
        Dim lifeTime As TimeSpan
        Dim lifeDays As Integer

        birthDay = Date.Parse("December 25, 2007")
        lifeTime = Now.Subtract(birthDay)
        lifeDays = lifeTime.Days
        Console.WriteLine(String.Format( _
           "There are {0} days between {1:D} and {2:D}", _
           lifeDays, birthDay, Now))
    End Sub
End Class
There are -227 days between December 25, 2007 and May 11, 2007