0

I have a string which contains the date with time and timezone in it. I want to convert it to datetime with the same timezone given in string.

I have already tried converting string to datetime but it converts datetime to my system timezone which is different from the one given in the string.

Convert.ToDateTime("2019-01-20T08:30:00+02:00") 

Converts it to my local timezone which is +05:30.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Dipen Shah
  • 191
  • 1
  • 2
  • 6
  • in which format u want to print your date – Dhanil Dinesan Jul 10 '19 at 05:42
  • 1
    You can extract Time Offset as suggested in @Salah Akbari's answer, but you cannot determine a Time Zone. More details here: https://stackoverflow.com/questions/22159386/is-there-any-delivered-method-which-takes-in-timezoneoffset-of-utc-and-calculate – felix-b Jul 10 '19 at 06:06

1 Answers1

2

You can use DateTimeOffset for this purpose, instead of the DateTime:

DateTimeOffset dateTime = DateTimeOffset.Parse("2019-01-20T08:30:00+02:00",
    CultureInfo.InvariantCulture);

Just don't forget to add the following to your using statements:

using System.Globalization;
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    It worth noting that you are talking about Time Offset, and not Time Zone. The difference: most time zones change their offset twice a year (daylight saving). Time zones are represented by TimeZoneInfo class in .NET, and are identified by a string id. – felix-b Jul 10 '19 at 06:01