简体   繁体   中英

C# (Windows Phone Silverlight) String to DateTime

I have a string like 2015-07-30T11:11:00+0200 . How can I parse it to DateTime object? DateTime.Parse(string) throws null exception , ParseExact too.

I would parse it to DateTimeOffset instead of DateTime since your string has UTC offset part.

string s = "2015-07-30T11:11:00+0200";
DateTimeOffset dt;
if(DateTimeOffset.TryParseExact(s, "yyyy-MM-dd'T'HH:mm:ssK", CultureInfo.InvariantCulture,
                                DateTimeStyles.None, out dt))
{
    //
}

Now, you have a DateTimeOffset as

30.07.2015 11:11:00 +02:00

If you wanna get it's DateTime part, you can use it's DateTimeOffset.DateTime property which returns;

30.07.2015 11:11:00 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM