Posługiwanie się kulturą Górnołużycki (hsb) obiekt DateTime przekonwertowany na ciąg znaków ma format "d. M. yyyy H.mm.ss" hodź. "". ToString ("G") na przykład zwraca "31. 12. 2011 5.06.07 hodź." na 31 grudnia 2011, 05:06:07..NET 4.5 Format daty/konwersji błędu z kulturą Górnołużycki
Problem polega jednak na tym, że próba przekonwertowania takiego ciągu z powrotem na DateTime nie jest prawdą. Nawet prostsze ciągi, takie jak "1. 1. 2011" lub "1.1.2011", nie przynoszą rezultatu. I na wszelki wypadek, gdyby ktoś sugerował przekazanie kultury podczas konwersji/persingu: zrobiłem to oczywiście.
Próba przeanalizowania "1.2.3" powoduje wyświetlenie aktualnej daty z czasem 01:02:03.
Uważam, że błąd. A może ktoś wie, co może być nie tak?
Używam .NET 4.5 RTM na komputerze z systemem Windows 8 RTM.
Próbka:
DateTime date = DateTime.Now;
CultureInfo culture = new CultureInfo("hsb");
string dateString = date.ToString("G", culture);
DateTime convertedDate;
bool dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results false although the date string was read by
// ToString("G") (i.e. '20. 9. 2012 12.28.10 hodź.') and should be okay
dateString = "1. 1. 2000";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results in false although the date string should be okay
dateString = "1.1.2000";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay);
// This results also in false
dateString = "1.2.3";
dateOkay = DateTime.TryParse(dateString, culture,
DateTimeStyles.AllowInnerWhite, out convertedDate);
Console.WriteLine(dateOkay + ": " + convertedDate);
// This results strangely in true. The converted date is the current date
// with time 01:02:03.
Hmm. Twój pierwszy przykład działa dla mnie, z Windows 7, ale z wykorzystaniem .NET 4.5 RTM. Czy możesz zmienić to w krótką, ale kompletną aplikację konsolową? –
Komentujący wskazują poprawne odpowiedzi, ale żadne z pytań nie są oznaczone jako odpowiedzi. Wiesz, że czek wskazuje poprawną odpowiedź. – Paparazzi
@Jon: Brzmi jak problem w Windows 8 (Microsoft zmienił wiele formatów daty). Oto uproszczony, ale działający kod aplikacji konsoli: CultureInfo culture = new CultureInfo ("hsb"); DateTime date = new DateTime (2011, 12, 31, 5, 6, 7); string dateString = date.ToString ("G", kultura); DateTime skonwertowaneData; bool dateOkay = DateTime.TryParse (dataString, kultura, DateTimeStyles.AllowInnerWhite, out skonwertowanyData); Console.WriteLine (dateOkay); dateString = "1. 1. 2000"; dateOkay = DateTime.TryParse (dataString, kultura, DateTimeStyles.AllowInnerWhite, out skonwertowanyData); –