Qualcuno ha idea del perché questo non funziona? Sono stato in grado di aggirarlo con ParseExact, ma mi piacerebbe capire perché sta fallendo.C# DateTime.Parse Errore
DateTime test = DateTime.Parse("Dec 24 17:45");
Date < "24 dicembre" funzionano correttamente. Date> = 24 Dic riuscire con questo errore:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
EDIT: Grazie a Habib per notare anche quando non ho ottenuto un errore che non era il risultato che mi aspettavo. Quindi fai attenzione con DateTime.Parse quando non viene utilizzato con i formati supportati!
Ecco cosa ho fatto per risolvere il problema. Devo solo gestire due diversi formati. L'anno in corso sarebbe "MMM GG HH: MM" altrimenti sarebbe "MMM GG AAAA"
if (!DateTime.TryParseExact(inDateTime, "MMM dd HH:mm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces,out outDateTime))
{
if (!DateTime.TryParseExact(inDateTime, "MMM dd yyyy", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out outDateTime))
{
//Handle failure to Parse
}
}
Puoi mostrarci il codice esatto che hai provato ad eseguire? –
Questa è una buona domanda. –
@ A.Abramov - L'OP includeva praticamente tutto ciò che è necessario. –