1

I have been searching to convert string to datetime with ignoring the offset. I have a string with local date time as 2017-02-13T12:11:03.303 +01:00 i want to ignore the offset part and string should be converted to 2017-02-13 12:11:03.303 as datetime format. I searched google but could not find one.

Referred How to convert string to DateTime in C#?

Not a duplicate of DateTime.ParseExact, Ignore the timezone as while searching didn't know its realted to DateTimeOffset.Searched using layman terms and no proper result found.

SidD
  • 5,697
  • 4
  • 18
  • 30
  • You _can't_ have a `2017-02-13T12:11:03.303 +01:00` as a `DateTime`. A `DateTime` instance doesn't have a UTC Offset. – Soner Gönül Apr 23 '18 at 15:04
  • @Soner I don’t think the request was for “2017-02-13T12:11:03.303 +01:00 as a DateTime”. I believe he states he has that as a string and wants it as a DateTime without the offset. I also don’t think this is a duplicate as the linked answer returns a DateTimeOffset where this request is for a DateTime. It seems mm8’s answer is correct and complete for this question. – BernieP Apr 23 '18 at 15:17

1 Answers1

3

Use the DateTimeOffset.Parse method:

string s = "2017-02-13T12:11:03.303 +01:00";
DateTime dt = DateTimeOffset.Parse(s, null).DateTime;
mm8
  • 163,881
  • 10
  • 57
  • 88