' This code example demonstrates the result yielded by each custom
' DateTime format specifier.
Imports System
Imports System.Globalization
Class Sample
Public Shared Sub Main()
Dim formatString As String
Dim ci As New CultureInfo("")
Dim msgDTctor As String = "The DateTime constructor for " & _
"January 3, 0001 A.D., 02:09:20.444 P.M. is" & vbLf & _
" DateTime(0001, 1, 3, 14, 9, 20, 444)" & vbLf
Dim thisDate As New DateTime(1, 1, 3, 14, 9, 20, 444)
Dim utcDate As New DateTime(0002, 2, 4, 15, 10, 21, 555, DateTimeKind.Utc)
Dim localDate As New DateTime(0003, 3, 5, 16, 11, 22, 666, DateTimeKind.Local)
' Custom DateTime format specifiers.
' The specifiers are for year, month, day, era, hour, minute, second, fraction of a second,
' A.M./P.M. designator, UTC offset, quoted string (apostrophe), quoted string (quotation mark),
' and escape character.
Dim specifiers As String() = New String() { _
"%y", "yy", "yyy", "yyyy", "yyyyy", _
"%M", "MM", "MMM", "MMMM", _
"%d", "dd", "ddd", "dddd", _
"%g", "gg", _
"%h", "hh", "%H", "HH", _
"%m", "mm", _
"%s", "ss", _
"%f", "ff", "fff", "ffff", "fffff", "ffffff", "fffffff", _
"%F", "FF", "FFF", "FFFF", "FFFFF", "FFFFFF", "FFFFFFF", _
"%t", "tt", _
"%z", "zz", "zzz", _
"'The year is' yyyy", _
"""The year is"" yyyy", _
"\\"}
Console.Clear()
Console.WriteLine("The culture is {0}.", ci.DisplayName)
Console.WriteLine(msgDTctor)
Console.WriteLine("{0,20} {1}", "Custom format string", "Yields")
Console.WriteLine("{0,20} {1}", "--------------------", "------")
Dim format As String
For Each format In specifiers
formatString = """" & format & """"
Console.WriteLine("{0,20} ""{1}""", formatString, thisDate.ToString(format))
Next format
Console.WriteLine()
Console.WriteLine()
' Example of using "K" format specifier.
Console.WriteLine("Format different kinds of DateTime using ""K""")
Console.WriteLine("{0, 20} {1}", "DateTimeKind", "Yields")
Console.WriteLine("{0,20} {1}", "------------", "------")
Console.WriteLine("{0,20} ""{1}""", thisDate.Kind, thisDate.ToString("%K"))
Console.WriteLine("{0,20} ""{1}""", utcDate.Kind, utcDate.ToString("%K"))
Console.WriteLine("{0,20} ""{1}""", localDate.Kind, localDate.ToString("%K"))
End Sub 'Main
End Class 'Sample
'This code example produces the following results:
'
'The culture is Invariant Language (Invariant Country).
'The DateTime constructor for January 3, 0001 A.D., 02:09:20.444 P.M. is
' DateTime(0001, 1, 3, 14, 9, 20, 444)
'
'Custom format string Yields
'-------------------- ------
' "%y" "1"
' "yy" "01"
' "yyy" "001"
' "yyyy" "0001"
' "yyyyy" "00001"
' "%M" "1"
' "MM" "01"
' "MMM" "Jan"
' "MMMM" "January"
' "%d" "3"
' "dd" "03"
' "ddd" "Wed"
' "dddd" "Wednesday"
' "%g" "A.D."
' "gg" "A.D."
' "%h" "2"
' "hh" "02"
' "%H" "14"
' "HH" "14"
' "%m" "9"
' "mm" "09"
' "%s" "20"
' "ss" "20"
' "%f" "4"
' "ff" "44"
' "fff" "444"
' "ffff" "4440"
' "fffff" "44400"
' "ffffff" "444000"
' "fffffff" "4440000"
' "%F" "4"
' "FF" "44"
' "FFF" "444"
' "FFFF" "444"
' "FFFFF" "444"
' "FFFFFF" "444"
' "FFFFFFF" "444"
' "%t" "P"
' "tt" "PM"
' "%z" "-8"
' "zz" "-08"
' "zzz" "-08:00"
'"'The year is' yyyy" "The year is 0001"
'""The year is" yyyy" "The year is 0001"
' "\\" "\"
'
'
'Format different kinds of DateTime using "K"
' DateTimeKind Yields
' ------------ ------
' Unspecified ""
' Utc "Z"
' Local "-08:00"
|