This is the string: 2007-08-31T06:59:40+02:00
How do I convert it to DateTime
?
DateTimeOffset also handles the time zone offset:
DateTimeOffset.Parse("2007-08-31T06:59:40+02:00")
or
DateTimeOffset.ParseExact("2007-08-31T06:59:40+02:00", "yyyy-MM-ddTHH:mm:sszzzz"
,System.Globalization.CultureInfo.InvariantCulture));
You should use DateTimeOffset.ParseExact
- you know the exact format you're expecting, and as this presumably has come from another computer rather than user input, it's a definite error if it fails. Likewise you should specify the invariant culture to indicate that this does not depend on the "current culture" of the executing thread.
var value = DateTimeOffset.ParseExact(text,
"yyyy'-'MM'-'dd'T'HH':'mm':'sszzz",
CultureInfo.InvariantCulture);
DateTimeOffset
is the type that accurately represents the information - DateTime
would have to lose some of that information.
使用DateTime.Parse()
:
DateTime dt = DateTime.Parse ("2007-08-31T06:59:40+02:00")
You could use DateTimeOffset.TryParse Method.
Use DateTimeOffset.Parse()
.
Then use either DateTime
property or DateTimeUtc
property to get a DateTime (but you should investigate if you could keep using DateTimeOffset.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.