Ciąg C # do DateTime ze strefą czasową
Chcę sformatować łańcuch: „2012-04-20 10: 10: 00 + 0200” do daty w tym formacie. więc myślę, że musi to być „rrrr-MM-dd hh: mm: ss zzz”?
kiedy to spróbowałem
<code> // starttime = {20/04/2012 10:10:00} without my +0200! DateTime starttime = Convert.ToDateTime("2012-04-20 10:10:00+0200",CultureInfo.CurrentCulture); // And this gave me a format exception : {System.FormatException: String was not recognized as a valid DateTime. DateTime result = DateTime.ParseExact("2012-04-20 10:10:00+0200", "yyyy-MM-dd hh:mm:ss zzz", CultureInfo.InvariantCulture); </code>
ROZWIĄZANIE PODANE PRZEZ „V4 Vendetta”:
Powinieneś spróbować użyć DateTimeOffset zamiast DateTime
<code>DateTimeOffset result = DateTimeOffset.Parse("2012-04-20 10:10:00+0200",CultureInfo.InvariantCulture); </code>
Tutaj otrzymasz również przesunięcie (2 godziny), które można obliczyć za pomocą wartości DateTime (10:10) i wstawić żądane wyjście (result.DateTime + result.Offset)