VB.Net Tutorial/Date Time/Date Format

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

Date format: {0:d} ;{1:D}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:d} ;{1:D} ", DateTime.Now, DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:30 PM
11/05/2007 ;May 11, 2007

Date format: {0:dddd},

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim weekDay As Integer = rightNow.DayOfWeek
        Dim weekDayShort As String = Format(rightNow, "ddd")
        Dim weekDayLong As String = String.Format("{0:dddd}", rightNow)
        Dim results As String = String.Format( _
           "Today"s day of the week: {0}, or {1}, or {2}", _
           weekDay, weekDayShort, weekDayLong)
        Console.WriteLine(results)
    End Sub
End Class
Today"s day of the week: 5, or Fri, or Friday

Date format: {0:f} ;{1:F}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:f} ;{1:F} ", DateTime.Now, DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:31 PM
May 11, 2007 9:23 PM ;May 11, 2007 9:23:31 PM

Date format: {0:g} ;{1:G}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:g} ;{1:G} ", DateTime.Now, DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:31 PM
11/05/2007 9:23 PM ;11/05/2007 9:23:31 PM

Date format: {0:m} ;{1:M}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:m} ;{1:M} ", DateTime.Now, DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:32 PM
May 11 ;May 11

Date format: {0:MMMM-dd-yy HH:mm:ss}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:MMMM-dd-yy  HH:mm:ss}", DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:33 PM
May-11-07  21:23:33

Date format: {0:t} ;{1:T}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:t} ;{1:T} ", DateTime.Now, DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:31 PM
9:23 PM ;9:23:31 PM

Date format: {0:yyyy/MM/dd HH:mm:ss}

Public Class Tester
    Public Shared Sub Main
        Console.WriteLine(DateTime.Now.ToString)
        Console.WriteLine(String.Format("{0:yyyy/MM/dd HH:mm:ss}", DateTime.Now))
    End Sub
End Class
11/05/2007 9:23:32 PM
2007/05/11 21:23:32

Date format: dd

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.Append("dd: ").AppendLine(rightNow.ToString("dd"))
        Console.WriteLine(result.ToString)
    End Sub
End Class
dd: 11

Date format: ddd

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.Append("ddd: ").AppendLine(rightNow.ToString("ddd"))
        Console.WriteLine(result.ToString)
    End Sub
End Class
ddd: Fri

Date format: dddd

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.Append("dddd: ").AppendLine(rightNow.ToString("dddd"))
        Console.WriteLine(result.ToString)
    End Sub
End Class
dddd: Friday

Date format: HH:mm:ss.fff z

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.Append("HH:mm:ss.fff z: ")
        result.AppendLine(rightNow.ToString("HH:mm:ss.fff z"))
        Console.WriteLine(result.ToString)
    End Sub
End Class
HH:mm:ss.fff z: 21:09:29.265 -7

Date format: s, u, Z

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now.ToUniversalTime
        Dim format1 As String = rightNow.ToString("s")
        Dim format2 As String = rightNow.ToString("u")
        Dim format3 As String = rightNow.ToString("s") & "Z"
        Dim format4 As String = rightNow.ToString( _
           "u").Substring(0, 19)
        Console.WriteLine(String.Format( _
           "s:  {1}{0}u:  {2}{0}T&Z: {3}{0}Neither: {4}", _
           vbNewLine, format1, format2, format3, format4))

    End Sub
End Class
s:  2007-05-12T04:09:30
u:  2007-05-12 04:09:30Z
T&Z: 2007-05-12T04:09:30Z
Neither: 2007-05-12 04:09:30

Date format: yy/MM/dd g

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.Append("yy/MM/dd g: ")
        result.AppendLine(rightNow.ToString("yy/MM/dd g"))
        Console.WriteLine(result.ToString)
    End Sub
End Class
yy/MM/dd g: 07/05/11 A.D.

Use format specifiers to control the date display

Public Class Tester
    Public Shared Sub Main
        Dim rightNow As Date = Now
        Dim result As New System.Text.StringBuilder
        result.AppendLine("""Now""...")
        " ----- Use format specifiers to control the date display.
        result.Append("d: ").AppendLine(rightNow.ToString("d"))
        result.Append("D: ").AppendLine(rightNow.ToString("D"))
        result.Append("t: ").AppendLine(rightNow.ToString("t"))
        result.Append("T: ").AppendLine(rightNow.ToString("T"))
        result.Append("f: ").AppendLine(rightNow.ToString("f"))
        result.Append("F: ").AppendLine(rightNow.ToString("F"))
        result.Append("g: ").AppendLine(rightNow.ToString("g"))
        result.Append("G: ").AppendLine(rightNow.ToString("G"))
        result.Append("M: ").AppendLine(rightNow.ToString("M"))
        result.Append("R: ").AppendLine(rightNow.ToString("R"))
        result.Append("s: ").AppendLine(rightNow.ToString("s"))
        result.Append("u: ").AppendLine(rightNow.ToString("u"))
        result.Append("U: ").AppendLine(rightNow.ToString("U"))
        result.Append("y: ").AppendLine(rightNow.ToString("y"))

        Console.WriteLine(result.ToString)
    End Sub
End Class
Now"...
d: 11/05/2007
D: May 11, 2007
t: 9:09 PM
T: 9:09:27 PM
f: May 11, 2007 9:09 PM
F: May 11, 2007 9:09:27 PM
g: 11/05/2007 9:09 PM
G: 11/05/2007 9:09:27 PM
M: May 11
R: Fri, 11 May 2007 21:09:27 GMT
s: 2007-05-11T21:09:27
u: 2007-05-11 21:09:27Z
U: May 12, 2007 4:09:27 AM
y: May, 2007