Pada .Net memiliki standard Date time format, dimana date time format tersebut dapat kita ubah ubah sesuka kita sesuai dengan kebutuhan, baik dikarenakan bentuk penulisan penanggalan yang berbeda beda.
Pattern | Fungsi | Contoh |
d | Short date Format | 5/19/1990 |
D | Long date Format | Sunday, August 15,1990 |
t | Short time Format | 3:33 PM |
T | Long time Format | 3:30:00 PM |
f | Full date/time format (short time) | Sunday, August 22,1990 |
F | Full date/time format (long time) | Sunday, August 22,1990 3:30:00 PM |
g | General date/time format (short time) | 3/30/1990 3:30 PM |
G | General date/time format (long time) | 3/30/1990 3:30:00 PM |
m or M | Month day format | August 30 |
r or R | RFC 1123 | Sun, 30 Aug 1990 3:30:00 GMT |
s | Sortable date /time format | 1990-08027T15:30:00 |
u | Universable sortable date/time format | 1990-08-27 15:30:30:00z |
U | Universable sortable date/time format | Sunday, August 30,1990 12:30:00 PM |
y or Y | Year month format | August, 1990 |
Contoh penggunaan kode dari Standar DateTime Format String
Imports System
Imports System.Globalization
Imports System.Threading
Class Sample
Public Shared Sub Main()
Dim msgShortDate As String = "(d) Short date: . . . . . . . "
Dim msgLongDate As String = "(D) Long date:. . . . . . . . "
Dim msgShortTime As String = "(t) Short time: . . . . . . . "
Dim msgLongTime As String = "(T) Long time:. . . . . . . . "
Dim msgFullDateShortTime As String = _
"(f) Full date/short time: . . "
Dim msgFullDateLongTime As String = _
"(F) Full date/long time:. . . "
Dim msgGeneralDateShortTime As String = _
"(g) General date/short time:. "
Dim msgGeneralDateLongTime As String = _
"(G) General date/long time (default):" & vbCrLf & _
" . . . . . . . . . . . . . "
Dim msgMonth As String = "(M) Month:. . . . . . . . . . "
Dim msgRFC1123 As String = "(R) RFC1123:. . . . . . . . . "
Dim msgSortable As String = "(s) Sortable: . . . . . . . . "
Dim msgUniSortInvariant As String = _
"(u) Universal sortable (invariant):" & vbCrLf & _
" . . . . . . . . . . . . . "
Dim msgUniSort As String = "(U) Universal sortable: . . . "
Dim msgYear As String = "(Y) Year: . . . . . . . . . . "
Dim msgRoundtripLocal As String = "(o) Roundtrip (local):. . . . "
Dim msgRoundtripUTC As String = "(o) Roundtrip (UTC):. . . . . "
Dim msgRoundtripUnspecified As String = "(o) Roundtrip (Unspecified):. "
Dim msg1 As String = "Use ToString(String) and the current thread culture." & vbCrLf
Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." & vbCrLf
Dim msgCulture As String = "Culture:"
Dim msgThisDate As String = "This date and time: {0}" & vbCrLf
Dim thisDate As DateTime = DateTime.Now
Dim utcDate As DateTime = thisDate.ToUniversalTime()
Dim unspecifiedDate As DateTime = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified)
Dim ci As CultureInfo
' Format the current date and time in various ways.
Console.Clear()
Console.WriteLine("Standard DateTime Format Specifiers:" & vbCrLf)
Console.WriteLine(msgThisDate, thisDate)
Console.WriteLine(msg1)
' Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture
Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)
Console.WriteLine(msgShortDate & thisDate.ToString("d"))
Console.WriteLine(msgLongDate & thisDate.ToString("D"))
Console.WriteLine(msgShortTime & thisDate.ToString("t"))
Console.WriteLine(msgLongTime & thisDate.ToString("T"))
Console.WriteLine(msgFullDateShortTime & thisDate.ToString("f"))
Console.WriteLine(msgFullDateLongTime & thisDate.ToString("F"))
Console.WriteLine(msgGeneralDateShortTime & thisDate.ToString("g"))
Console.WriteLine(msgGeneralDateLongTime & thisDate.ToString("G"))
Console.WriteLine(msgMonth & thisDate.ToString("M"))
Console.WriteLine(msgRFC1123 & utcDate.ToString("R"))
Console.WriteLine(msgSortable & thisDate.ToString("s"))
Console.WriteLine(msgUniSortInvariant & utcDate.ToString("u"))
Console.WriteLine(msgUniSort & thisDate.ToString("U"))
Console.WriteLine(msgYear & thisDate.ToString("Y"))
Console.WriteLine(msgRoundtripLocal & thisDate.ToString("o"))
Console.WriteLine(msgRoundtripUTC & utcDate.ToString("o"))
Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"))
Console.WriteLine()
' Display the same values using a CultureInfo object. The CultureInfo class
' implements IFormatProvider.
Console.WriteLine(msg2)
' Display the culture used to format the values.
ci = New CultureInfo("de-DE")
Console.WriteLine("{0,-30}{1}" & vbCrLf, msgCulture, ci.DisplayName)
Console.WriteLine(msgShortDate & thisDate.ToString("d", ci))
Console.WriteLine(msgLongDate & thisDate.ToString("D", ci))
Console.WriteLine(msgShortTime & thisDate.ToString("t", ci))
Console.WriteLine(msgLongTime & thisDate.ToString("T", ci))
Console.WriteLine(msgFullDateShortTime & thisDate.ToString("f", ci))
Console.WriteLine(msgFullDateLongTime & thisDate.ToString("F", ci))
Console.WriteLine(msgGeneralDateShortTime & thisDate.ToString("g", ci))
Console.WriteLine(msgGeneralDateLongTime & thisDate.ToString("G", ci))
Console.WriteLine(msgMonth & thisDate.ToString("M", ci))
Console.WriteLine(msgRFC1123 & utcDate.ToString("R", ci))
Console.WriteLine(msgSortable & thisDate.ToString("s", ci))
Console.WriteLine(msgUniSortInvariant & utcDate.ToString("u", ci))
Console.WriteLine(msgUniSort & thisDate.ToString("U", ci))
Console.WriteLine(msgYear & thisDate.ToString("Y", ci))
Console.WriteLine(msgRoundtripLocal & thisDate.ToString("o"), ci)
Console.WriteLine(msgRoundtripUTC & utcDate.ToString("o"), ci)
Console.WriteLine(msgRoundtripUnspecified & unspecifiedDate.ToString("o"), ci)
Console.WriteLine()
End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard DateTime Format Specifiers:
'
'This date and time: 4/17/2006 2:29:09 PM
'
'Use ToString(String) and the current thread culture.
'
'Culture: English (United States)
'
'(d) Short date: . . . . . . . 4/17/2006
'(D) Long date:. . . . . . . . Monday, April 17, 2006
'(t) Short time: . . . . . . . 2:29 PM
'(T) Long time:. . . . . . . . 2:29:09 PM
'(f) Full date/short time: . . Monday, April 17, 2006 2:29 PM
'(F) Full date/long time:. . . Monday, April 17, 2006 2:29:09 PM
'(g) General date/short time:. 4/17/2006 2:29 PM
'(G) General date/long time (default):
' . . . . . . . . . . . . . 4/17/2006 2:29:09 PM
'(M) Month:. . . . . . . . . . April 17
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
' . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal sortable: . . . Monday, April 17, 2006 9:29:09 PM
'(Y) Year: . . . . . . . . . . April, 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture: German (Germany)
'
'(d) Short date: . . . . . . . 17.04.2006
'(D) Long date:. . . . . . . . Montag, 17. April 2006
'(t) Short time: . . . . . . . 14:29
'(T) Long time:. . . . . . . . 14:29:09
'(f) Full date/short time: . . Montag, 17. April 2006 14:29
'(F) Full date/long time:. . . Montag, 17. April 2006 14:29:09
'(g) General date/short time:. 17.04.2006 14:29
'(G) General date/long time (default):
' . . . . . . . . . . . . . 17.04.2006 14:29:09
'(M) Month:. . . . . . . . . . 17 April
'(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:29:09 GMT
'(s) Sortable: . . . . . . . . 2006-04-17T14:29:09
'(u) Universal sortable (invariant):
' . . . . . . . . . . . . . 2006-04-17 21:29:09Z
'(U) Universal sortable: . . . Montag, 17. April 2006 21:29:09
'(Y) Year: . . . . . . . . . . April 2006
'(o) Roundtrip (local):. . . . 2006-04-17T14:29:09.3011250-07:00
'(o) Roundtrip (UTC):. . . . . 2006-04-17T21:29:09.3011250Z
'(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
No comments:
Post a Comment