VB.Net Tutorial/Date Time/DateTime

Материал из VB Эксперт
Версия от 12:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DateTime.Parse(PM)

public class Test
   public Shared Sub Main
        Console.WriteLine(DateTime.Parse("12:00:00 PM"))
   End Sub
End class
11/05/2007 12:00:00 PM

DateTime parse date and time by DateTimeFormatInfo

public class Test
   public Shared Sub Main
        Dim Provider As New System.Globalization.DateTimeFormatInfo()
        Console.WriteLine(Provider.AMDesignator())
        Console.WriteLine(DateTime.ParseExact("12:42", "hh:mm", Provider))
   End Sub
End class
AM
11/05/2007 12:42:00 AM

DateTime Subtract

public class Test
   public Shared Sub Main
    Dim D As DateTime
    D = Now()
    Console.WriteLine(D.ToUniversalTime().GetType().Name)
    Console.WriteLine("UTC-LocalTime=" + D.ToUniversalTime().Subtract(D).ToString())
   End Sub
End class
DateTime
UTC-LocalTime=07:00:00

Get Date from long integer

public class Test
   public Shared Sub Main
    Dim D As New DateTime(10000333333333333)
    Console.WriteLine("10000 ticks is " + D.Date())
   End Sub
End class
10000 ticks is 09/09/0032

Get Hour from DateTime

Imports System
Public Class Greeting
     Shared Sub Main()
        Dim dtCurrent As System.DateTime
        Dim iHour As Integer
        dtCurrent = dtCurrent.Now()
        iHour = dtCurrent.Hour
        If (iHour < 12) Then
            Console.Writeline("Good Morning!")
        End If
        If (iHour >= 12) And (iHour < 18) Then
            Console.WriteLine("Good Afternoon!")
        End If
        If (iHour >= 18) Then
            Console.WriteLine("Good Evening!")
        End If
    End Sub
End Class
Good Afternoon!

Get UniversalTime from DateTime

public class Test
   public Shared Sub Main
        Dim D As DateTime
        D = Now()
    
        Console.WriteLine(D.ToUniversalTime().GetType().Name)
    
        Console.WriteLine("UTC-LocalTime=" + D.ToUniversalTime().Subtract(D).ToString())
    
        Console.WriteLine("Ticks since 12:00AM January 1, 1 CE="+ Now().Ticks().ToString())
   End Sub
End class
DateTime
UTC-LocalTime=07:00:00
Ticks since 12:00AM January 1, 1 CE=633144833635000000

Timing code block

public class Test
   public 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
0.015625
0.015625