1

How can i convert ISO 8601 with timezone to c# actual (local) datetime format.

my ISO 8601 date is like "2016-06-19T19:30:43+04:30"

and I need something like this "2016-06-19 16:00:43"

user861594
  • 5,733
  • 3
  • 29
  • 45
  • Possible duplicate of [How to create a .NET DateTime from ISO 8601 format](http://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format) – Claies Jun 19 '16 at 10:35
  • i have used it, but it raised error. since in this sample date format is different than mine. it is "2010-08-20T15:00:00Z" while my date has timezone "2016-06-19T19:30:43+04:30" – Maryam Mohamadi Jun 19 '16 at 11:28

1 Answers1

0

since W3c adopted ISO8601 you can use:

XmlConvert.ToString Method (DateTime, XmlDateTimeSerializationMode)

with the XmlDateTimeSerializationMode you can control how you want to date to be processed.

see https://msdn.microsoft.com/en-us/library/ms162344(v=vs.110).aspx

and there is the reverse method to convert string to date...

XmlConvert.ToDateTime Method (String, XmlDateTimeSerializationMode)

https://msdn.microsoft.com/en-us/library/ms162342(v=vs.110).aspx

Phil Blackburn
  • 1,047
  • 1
  • 8
  • 13
  • i don't want to convert date to string. i need datetime but in c# normal datetime, without any T or Z or timezone value! – Maryam Mohamadi Jun 19 '16 at 12:01
  • thanks for your answer but it returns error :The string '2016-06-19T21:57:43 04:30' is not a valid AllXsd value. – Maryam Mohamadi Jun 19 '16 at 12:57
  • your missing the + for the timezone. – Phil Blackburn Jun 19 '16 at 15:53
  • That's right but why? when i check the variable in javascript, i see that it has + before timezone but when i catch it in codebehind, it is a space instead of + !! – Maryam Mohamadi Jun 20 '16 at 04:52
  • Are you passing the time in a url query string? If you are you need to encode the + first because the + is interpreted as a space. We're now moving away from the subject of this question. I'm sure an SO search will find questions covering this new issue – Phil Blackburn Jun 20 '16 at 08:35