whether time is based on local time, Coordinated Universal Time (UTC), or neither. : Date Operation « Date Time « VB.Net






whether time is based on local time, Coordinated Universal Time (UTC), or neither.

  

Imports System

Class Sample
    Public Shared Sub Main() 
        Dim saveNow As DateTime = DateTime.Now
        Dim saveUtcNow As DateTime = DateTime.UtcNow
        Dim myDt As DateTime
        DisplayNow("UtcNow:", saveUtcNow)
        DisplayNow("Now:", saveNow)

        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)
        Display("Utc:", myDt)
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Local)

        Display("Local:", myDt)
        myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified)
        Display("Unspecified:", myDt)
    End Sub 'Main
    Public Shared datePatt As String = "M/d/yyyy hh:mm:ss tt"
    Public Shared Sub Display(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dispDt As DateTime = inputDt
        Dim dtString As String

        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, dispDt.Kind)
        dispDt = inputDt.ToLocalTime()
        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("  ToLocalTime:     {0}, Kind = {1}", dtString, dispDt.Kind)
        dispDt = inputDt.ToUniversalTime()
        dtString = dispDt.ToString(datePatt)
        Console.WriteLine("  ToUniversalTime: {0}, Kind = {1}", dtString, dispDt.Kind)
    End Sub 'Display
    Public Shared Sub DisplayNow(ByVal title As String, ByVal inputDt As DateTime) 
        Dim dtString As String = inputDt.ToString(datePatt)
        Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, inputDt.Kind)
    End Sub
End Class

   
    
  








Related examples in the same category

1.Add time (days, hours) to current timeAdd time (days, hours) to current time
2.Date Time SubtractionDate Time Subtraction
3.Multiply Time SpanMultiply Time Span
4.Date Time and TimeSpan DemoDate Time and TimeSpan Demo
5.Add a day, a month and a year to a Date
6.Date = #11/4/2010 1:30:00AM#
7.CDate("10/26/2010 1:32:00 PM")
8.Arithmetic Operations with Dates and Times
9.Calculate the interval between the two dates
10.Display the date using the current (en-US) culture
11.Change the current culture to fr-FR and display the date
12.Change the current culture to ja-JP and display the date
13.Allow a leading space in the date string
14.Represents an instant in time, typically expressed as a date and time of day.
15.Create Date object from String
16.Date.Now, UtcNow and Today
17.Create Date from date string with time
18.Convert Date to string with fr-FR culture
19.Adds time interval to a specified date and time, yielding a new date and time.
20.Compare the difference between two Date value with Ticks
21.Compares two Date values
22.Gets the date component from String
23.Gets the day of the month from Date
24.Gets the milliseconds component of the date represented by this instance.
25.Ticks from a Date
26.Difference between local time and UTC
27.Display the ticks-per-time-unit fields
28.Add one and half Milliseconds
29.Gets the day of the week represented by this instance.
30.Returns the number of days in the specified month and year.
31.Find out the file age
32.Returns an indication whether the specified year is a leap year.
33.Reverse month and day to conform to the fr-FR culture