Stringhe di formato DateTime standard
Una stringa di formato DateTime standard è costituita da un singolo carattere identificatore di formato DateTime standard che rappresenta una stringa di formato DateTime personalizzato. La stringa di formato definisce in pratica la rappresentazione di testo di un oggetto DateTime derivante da un'operazione di formattazione. Le stringhe di formato DateTime contenenti più caratteri alfabetici, inclusi gli spazi, vengono interpretate come stringhe di formato DateTime personalizzato.
Identificatori di formato DateTime standard
Nella tabella riportata di seguito vengono descritti gli identificatori di formato DateTime standard. Per gli esempi dell'output prodotto da ogni identificatore di formato, vedere Esempi di output di stringhe di formato DateTime standard.
Identificatore di formato | Nome | Descrizione |
---|---|---|
d |
Schema di data breve |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà ShortDatePattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "MM/dd/yyyy". |
D |
Schema di data esteso |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà LongDatePattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "dddd, dd MMMM yyyy". |
f |
Schema di data/ora completo (ora breve) |
Rappresenta una combinazione dello schema di data esteso (D) e dello schema di ora breve (t), separati da uno spazio. |
F |
Schema di data/ora completo (ora estesa) |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà FullDateTimePattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "dddd, dd MMMM yyyy HH:mm:ss". |
g |
Schema di data/ora generale (ora breve) |
Rappresenta una combinazione dello schema di data breve (d) e dello schema di ora breve (t), separati da uno spazio. |
G |
Schema di data/ora generale (ora estesa) |
Rappresenta una combinazione dello schema di data breve (d) e dello schema di ora esteso (T), separati da uno spazio. |
M o m |
Schema mese giorno |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà MonthDayPattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "MMMM dd". |
o |
Schema di data/ora di riconversione |
Rappresenta una stringa di formato DateTime personalizzato con uno schema che mantiene le informazioni sul fuso orario. Lo schema è stato creato per convertire in testo i formati DateTime, inclusa la proprietà Kind. La stringa formattata può quindi essere rianalizzata utilizzando il metodo Parse o ParseExact con il valore della proprietà Kind corretto. La stringa di formato personalizzato è "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK". Lo schema per questo identificatore è uno standard definito e pertanto sarà sempre lo stesso, indipendentemente dalla lingua utilizzata o dal provider di formato fornito. |
R o r |
Schema RFC1123 |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà RFC1123Pattern corrente. Lo schema è uno standard definito e la proprietà è di sola lettura. Sarà pertanto sempre lo stesso, indipendentemente dalla lingua utilizzata o dal provider di formato fornito. La stringa di formato personalizzato è "ddd, dd MMM yyyy HH':'mm':'ss 'GMT". Con la formattazione il valore dell'oggetto DateTime che viene formattato non viene modificato. L'applicazione dovrà pertanto convertire il valore in formato UTC (Coordinated Universal Time) prima di utilizzare questo identificatore di formato. |
s |
Schema di data/ora ordinabile; conforme a ISO 8601 |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà SortableDateTimePattern corrente. Lo schema è uno standard definito e la proprietà è di sola lettura. Sarà pertanto sempre lo stesso, indipendentemente dalla lingua utilizzata o dal provider di formato fornito. La stringa di formato personalizzato è "yyyy'-'MM'-'dd'T'HH':'mm':'ss". |
t |
Schema di ora breve |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà ShortTimePattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "HH:mm". |
T |
Schema di ora esteso |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà LongTimePattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "HH:mm:ss". |
u |
Schema di data/ora ordinabile universale |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà UniversalSortableDateTimePattern corrente. Lo schema è uno standard definito e la proprietà è di sola lettura. Sarà pertanto sempre lo stesso, indipendentemente dalla lingua utilizzata o dal provider di formato fornito. La stringa di formato personalizzato è "yyyy'-'MM'-'dd HH':'mm':'ss'Z". Non viene eseguita alcuna conversione del fuso orario con la formattazione di data e ora. L'applicazione dovrà pertanto convertire la data e l'ora locali in formato UTC (Coordinated Universal Time) per utilizzare questo identificatore di formato. |
U |
Schema di data/ora ordinabile universale |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà FullDateTimePattern corrente. Questo schema corrisponde allo schema di data/ora completo (F). La formattazione tuttavia agisce sul valore UTC (Coordinated Universal Time) che equivale all'oggetto DateTime che viene formattato. |
Y o y |
Schema anno mese |
Rappresenta una stringa di formato DateTime personalizzato definita dalla proprietà YearMonthPattern corrente. La stringa di formato personalizzato per la lingua inglese ad esempio è "yyyy MMMM". |
Qualsiasi altro carattere singolo |
Identificatore sconosciuto |
Un identificatore sconosciuto genera un'eccezione di formato in fase di esecuzione. |
Impostazioni del Pannello di controllo
Le impostazioni di Opzioni internazionali e della lingua nel Pannello di controllo influiscono sulla stringa risultato prodotta da un'operazione di formattazione. Queste impostazioni vengono utilizzate per inizializzare l'oggetto DateTimeFormatInfo associato alla lingua del thread corrente, che fornisce i valori utilizzati per definire la formattazione. Computer con impostazioni diverse generano stringhe di risultati diverse.
Proprietà DateTimeFormatInfo
La formattazione è influenzata dalle proprietà dell'oggetto DateTimeFormatInfo corrente, che viene fornito in modo implicito dalla lingua del thread corrente o in modo esplicito dal parametro IFormatProvider del metodo che richiama la formattazione. Specificare per il parametro IFormatProvider un oggetto CultureInfo, che rappresenta una lingua, o un oggetto DateTimeFormatInfo.
Molti degli identificatori di formato DateTime standard sono alias di modelli di formattazione definiti da proprietà dell'oggetto DateTimeFormatInfo corrente. L'applicazione può pertanto modificare il risultato prodotto da alcuni identificatori di formato DateTime standard modificando la proprietà DateTimeFormatInfo corrispondente.
Utilizzo delle stringhe di formato standard
Nel frammento di codice riportato di seguito viene illustrato come utilizzare le stringhe di formato standard con oggetti DateTime.
Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")
' Create a new custom DateTime pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"
' Use the DateTimeFormat from the culture associated
' with the current thread.
Console.WriteLine( dt.ToString("d") )
Console.WriteLine( dt.ToString("m") )
' Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )
' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )
' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )
' Use a CultureInfo with a format specifier to parse a string.
Dim culter As New CultureInfo("en-US")
Dim myDateTime As DateTime
myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter)
Console.WriteLine(myDateTime.ToString("D"))
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");
// Create a new custom DateTime pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";
// Use the DateTimeFormat from the culture associated
// with the current thread.
Console.WriteLine( dt.ToString("d") );
Console.WriteLine( dt.ToString("m") );
// Use the DateTimeFormat from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );
// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );
// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );
// Use a CultureInfo with a format specifier to parse a string.
IFormatProvider culter = new CultureInfo("en-US");
DateTime myDateTime = DateTime.ParseExact("Tuesday, April 10, 2001", "D", culter);
Console.WriteLine(myDateTime.ToString("D"));
Esempio
Nell'esempio di codice riportato di seguito viene formattato un oggetto DateTime con la lingua corrente del thread, una lingua specificata e tutti gli identificatori di formato DateTime standard.
' This code example demonstrates the ToString(String) and
' ToString(String, IFormatProvider) methods for the DateTime
' type in conjunction with the standard date and time
' format specifiers.
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
// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for the DateTime
// type in conjunction with the standard date and time
// format specifiers.
using System;
using System.Globalization;
using System.Threading;
class Sample
{
public static void Main()
{
string msgShortDate = "(d) Short date: . . . . . . . ";
string msgLongDate = "(D) Long date:. . . . . . . . ";
string msgShortTime = "(t) Short time: . . . . . . . ";
string msgLongTime = "(T) Long time:. . . . . . . . ";
string msgFullDateShortTime =
"(f) Full date/short time: . . ";
string msgFullDateLongTime =
"(F) Full date/long time:. . . ";
string msgGeneralDateShortTime =
"(g) General date/short time:. ";
string msgGeneralDateLongTime =
"(G) General date/long time (default):\n" +
" . . . . . . . . . . . . . ";
string msgMonth = "(M) Month:. . . . . . . . . . ";
string msgRFC1123 = "(R) RFC1123:. . . . . . . . . ";
string msgSortable = "(s) Sortable: . . . . . . . . ";
string msgUniSortInvariant =
"(u) Universal sortable (invariant):\n" +
" . . . . . . . . . . . . . ";
string msgUniSort = "(U) Universal sortable: . . . ";
string msgYear = "(Y) Year: . . . . . . . . . . ";
string msgRoundtripLocal = "(o) Roundtrip (local):. . . . ";
string msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . ";
string msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. ";
string msg1 = "Use ToString(String) and the current thread culture.\n";
string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
string msgCulture = "Culture:";
string msgThisDate = "This date and time: {0}\n";
DateTime thisDate = DateTime.Now;
DateTime utcDate = thisDate.ToUniversalTime();
DateTime unspecifiedDate = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified);
CultureInfo ci;
// Format the current date and time in various ways.
Console.Clear();
Console.WriteLine("Standard DateTime Format Specifiers:\n");
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}\n", 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}\n", 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();
}
}
/*
This code example produces the following results:
Standard DateTime Format Specifiers:
This date and time: 4/17/2006 2:22:48 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:22 PM
(T) Long time:. . . . . . . . 2:22:48 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM
(g) General date/short time:. 4/17/2006 2:22 PM
(G) General date/long time (default):
. . . . . . . . . . . . . 4/17/2006 2:22:48 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal sortable: . . . Monday, April 17, 2006 9:22:48 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(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:22
(T) Long time:. . . . . . . . 14:22:48
(f) Full date/short time: . . Montag, 17. April 2006 14:22
(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48
(g) General date/short time:. 17.04.2006 14:22
(G) General date/long time (default):
. . . . . . . . . . . . . 17.04.2006 14:22:48
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal sortable: . . . Montag, 17. April 2006 21:22:48
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
*/
// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for the DateTime
// type in conjunction with the standard date and time
// format specifiers.
using namespace System;
using namespace System::Globalization;
using namespace System::Threading;
int main()
{
String^ msgShortDate = "(d) Short date: . . . . . . . ";
String^ msgLongDate = "(D) Long date:. . . . . . . . ";
String^ msgShortTime = "(t) Short time: . . . . . . . ";
String^ msgLongTime = "(T) Long time:. . . . . . . . ";
String^ msgFullDateShortTime =
"(f) Full date/short time: . . ";
String^ msgFullDateLongTime =
"(F) Full date/long time:. . . ";
String^ msgGeneralDateShortTime =
"(g) General date/short time:. ";
String^ msgGeneralDateLongTime =
"(G) General date/long time (default):\n" +
" . . . . . . . . . . . . . ";
String^ msgMonth = "(M) Month:. . . . . . . . . . ";
String^ msgRFC1123 = "(R) RFC1123:. . . . . . . . . ";
String^ msgSortable = "(s) Sortable: . . . . . . . . ";
String^ msgUniSortInvariant =
"(u) Universal sortable (invariant):\n" +
" . . . . . . . . . . . . . ";
String^ msgUniSort = "(U) Universal sortable: . . . ";
String^ msgYear = "(Y) Year: . . . . . . . . . . ";
String^ msgRoundtripLocal = "(o) Roundtrip (local):. . . . ";
String^ msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . ";
String^ msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. ";
String^ msg1 = "Use ToString(String) and the current thread culture.\n";
String^ msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
String^ msgCulture = "Culture:";
String^ msgThisDate = "This date and time: {0}\n";
DateTime^ thisDate = DateTime::Now;
DateTime^ utcDate = thisDate->ToUniversalTime();
DateTime^ unspecifiedDate = gcnew DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind::Unspecified);
CultureInfo^ ci;
// Format the current date and time in various ways.
Console::Clear();
Console::WriteLine("Standard DateTime Format Specifiers:\n");
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}\n", 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 = gcnew CultureInfo("de-DE");
Console::WriteLine("{0,-30}{1}\n", 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();
}
/*
This code example produces the following results:
Standard DateTime Format Specifiers:
This date and time: 4/17/2006 2:38: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:38 PM
(T) Long time:. . . . . . . . 2:38:09 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:38 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:38:09 PM
(g) General date/short time:. 4/17/2006 2:38 PM
(G) General date/long time (default):
. . . . . . . . . . . . . 4/17/2006 2:38:09 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:38:09
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:38:09Z
(U) Universal sortable: . . . Monday, April 17, 2006 9:38:09 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z
(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:38
(T) Long time:. . . . . . . . 14:38:09
(f) Full date/short time: . . Montag, 17. April 2006 14:38
(F) Full date/long time:. . . Montag, 17. April 2006 14:38:09
(g) General date/short time:. 17.04.2006 14:38
(G) General date/long time (default):
. . . . . . . . . . . . . 17.04.2006 14:38:09
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:38:09 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:38:09
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:38:09Z
(U) Universal sortable: . . . Montag, 17. April 2006 21:38:09
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:38:09.9417500-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:38:09.9417500Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000
*/
Vedere anche
Concetti
Cenni preliminari sulla formattazione
Stringhe di formato di data e ora
Esempi di output di stringhe di formato DateTime standard