Languages
[Edit]
EN

C# / .NET - convert iso 8601 to DateTime

4 points
Created by:
Root-ssh
6417

ISO 8601 precises different formats of date and time. In C# / .NET it is possible to convert iso 8601 string to DateTime object in few ways.

Quick solution:

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		CultureInfo culture = CultureInfo.InvariantCulture;
		
		string text = "2019-08-18T07:36:13+01:00";
		DateTime time = DateTime.Parse(text, culture, DateTimeStyles.None);

		Console.WriteLine(time.ToString(culture));
	}
}

Look below to see different examples:

1. Automatic format detection example

In this section we present how built-in method deals with different date time format.

Presented below code try to detect date time format and print result in console. As we can se some date-time formats are not supported by default configuration what was presented as PARSING ERROR! text. To solve the problem we can define custom list of supported formats and use them to detect format if error occured - check next section example to see how it works.

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		string[] dates = {
			"2019-08-18T07:36:13.5344729+01:00",

			"2019-08-18T07:36:13+01:00",
			"2019-08-18T07:36:13+01",
			"2019-08-18T07:36:13",
			"2019-08-18T07:36+01:00",
			"2019-08-18T07:36+01",
			"2019-08-18T07:36",
			"2019-08-18T07+01:00",
			"2019-08-18T07+01",
			"2019-08-18T07",
			"2019-08-18",

			"20190818T073613+01:00",
			"20190818T073613+01",
			"20190818T0736+01:00",
			"20190818T0736+01",
			"20190818T07+01:00",
			"20190818T07+01",
			"20190818T07",
			"20190818",

			"--08-18",

			"2019-08-18T06:36:13Z",
			"2019-08-18T06:36Z",
			"2019-08-18T06Z",

			"20190818T063613Z",
			"20190818T0636Z",
			"20190818T06Z"
		};

		CultureInfo culture = CultureInfo.InvariantCulture;

		foreach (string entry in dates)
		{
			DateTime time;

			if (DateTime.TryParse(entry, culture, DateTimeStyles.None, out time))
			{
				string text = time.ToString(culture);
				Console.WriteLine(string.Format("{0, -35} {1}", entry, text));
			}
			else
				Console.WriteLine(string.Format("{0, -35} {1}", entry, "PARSING ERROR!"));
		}
	}
}
Note: to parse date-time string it is possible to use DateTime.Parse method too, that throws exception if format is incorrect or not supported.

Output:

2019-08-18T07:36:13.5344729+01:00   08/18/2019 07:36:13

2019-08-18T07:36:13+01:00           08/18/2019 07:36:13
2019-08-18T07:36:13+01              08/18/2019 07:36:13
2019-08-18T07:36:13                 08/18/2019 07:36:13
2019-08-18T07:36+01:00              08/18/2019 07:36:00
2019-08-18T07:36+01                 08/18/2019 07:36:00
2019-08-18T07:36                    08/18/2019 07:36:00
2019-08-18T07+01:00                 PARSING ERROR!
2019-08-18T07+01                    PARSING ERROR!
2019-08-18T07                       PARSING ERROR!
2019-08-18                          08/18/2019 00:00:00

20190818T073613+01:00               PARSING ERROR!
20190818T073613+01                  PARSING ERROR!
20190818T0736+01:00                 PARSING ERROR!
20190818T0736+01                    PARSING ERROR!
20190818T07+01:00                   PARSING ERROR!
20190818T07+01                      PARSING ERROR!
20190818T07                         PARSING ERROR!
20190818                            PARSING ERROR!

--08-18                             PARSING ERROR!

2019-08-18T06:36:13Z                08/18/2019 07:36:13
2019-08-18T06:36Z                   08/18/2019 07:36:00
2019-08-18T06Z                      PARSING ERROR!
20190818T063613Z                    PARSING ERROR!
20190818T0636Z                      PARSING ERROR!
20190818T06Z                        PARSING ERROR!

2019-W34-0                          PARSING ERROR!
2019-W34                            PARSING ERROR!
2019-230                            PARSING ERROR!
Note: date-time values are parsed to local time what has been shown in output - read more about Z suffix here. 

2. Custom format example

There are another ways how to use custom formats to parse date-time string too.

When default parsing failed and we are sure that we use correct date time format we can define additional cases that are accepted.

Below dates were coppied from above example - so it is easy to detect witch are not supported with Parse method.

using System;
using System.Globalization;

public class Program
{
	public static void Main()
	{
		string[,] dates = {
			// FORMAT                    TEXT

			{ "yyyy-MM-ddTHH:mm:sszzz",   "2019-08-18T07:36:13+01:00" },
			{ "yyyy-MM-ddTHH:mm:sszz",    "2019-08-18T07:36:13+01"    },
			{ "yyyy-MM-ddTHH:mm:ss",      "2019-08-18T07:36:13"       },
			{ "yyyy-MM-ddTHH:mmzzz",      "2019-08-18T07:36+01:00"    },
			{ "yyyy-MM-ddTHH:mmzz",       "2019-08-18T07:36+01"       },
			{ "yyyy-MM-ddTHH:mm",         "2019-08-18T07:36"          },
			{ "yyyy-MM-ddTHHzzz",         "2019-08-18T07+01:00"       },
			{ "yyyy-MM-ddTHHzz",          "2019-08-18T07+01"          },
			{ "yyyy-MM-ddTHH",            "2019-08-18T07"             },
			{ "yyyy-MM-dd",               "2019-08-18"                },

			{ "yyyyMMddTHHmmsszzz",       "20190818T073613+01:00"     },
			{ "yyyyMMddTHHmmsszz",        "20190818T073613+01"        },
			{ "yyyyMMddTHHmmzzz",         "20190818T0736+01:00"       },
			{ "yyyyMMddTHHmmzz",          "20190818T0736+01"          },
			{ "yyyyMMddTHHzzz",           "20190818T07+01:00"         },
			{ "yyyyMMddTHHzz",            "20190818T07+01"            },
			{ "yyyyMMddTHH",              "20190818T07"               },
			{ "yyyyMMdd",                 "20190818"                  },

			{ "--MM-dd",                  "--08-18"                  },

			{ "yyyy-MM-ddTHH:mm:ssZ",     "2019-08-18T06:36:13Z"      },
			{ "yyyy-MM-ddTHH:mmZ",        "2019-08-18T06:36Z"         },
			{ "yyyy-MM-ddTHHZ",           "2019-08-18T06Z"            },

			{ "yyyyMMddTHHmmssZ",         "20190818T063613Z"          },
			{ "yyyyMMddTHHmmZ",           "20190818T0636Z"            },
			{ "yyyyMMddTHHZ",             "20190818T06Z"              }
		};

		CultureInfo culture = CultureInfo.InvariantCulture;
		int length = dates.GetLength(0);

		for (int i = 0; i < length; ++i)
		{
			string format = dates[i, 0];
			string text = dates[i, 1];

			DateTime time = DateTime.ParseExact(text, format, culture, DateTimeStyles.None);
			Console.WriteLine(string.Format("{0, -35} {1}", text, time.ToString(culture)));
		}
	}
}

Output:

2019-08-18T07:36:13+01:00           08/18/2019 07:36:13
2019-08-18T07:36:13+01              08/18/2019 07:36:13
2019-08-18T07:36:13                 08/18/2019 07:36:13
2019-08-18T07:36+01:00              08/18/2019 07:36:00
2019-08-18T07:36+01                 08/18/2019 07:36:00
2019-08-18T07:36                    08/18/2019 07:36:00
2019-08-18T07+01:00                 08/18/2019 07:00:00
2019-08-18T07+01                    08/18/2019 07:00:00
2019-08-18T07                       08/18/2019 07:00:00
2019-08-18                          08/18/2019 00:00:00

20190818T073613+01:00               08/18/2019 07:36:13
20190818T073613+01                  08/18/2019 07:36:13
20190818T0736+01:00                 08/18/2019 07:36:00
20190818T0736+01                    08/18/2019 07:36:00
20190818T07+01:00                   08/18/2019 07:00:00
20190818T07+01                      08/18/2019 07:00:00
20190818T07                         08/18/2019 07:00:00
20190818                            08/18/2019 00:00:00

--08-18                             08/18/2019 00:00:00

2019-08-18T06:36:13Z                08/18/2019 07:36:13
2019-08-18T06:36Z                   08/18/2019 07:36:00
2019-08-18T06Z                      08/18/2019 07:00:00

20190818T063613Z                    08/18/2019 07:36:13
20190818T0636Z                      08/18/2019 07:36:00
20190818T06Z                        08/18/2019 07:00:00
Note: date-time values are parsed to local time what has been shown in output - read more about Z suffix here. 

See also

  1. ISO 8601 - What is Z in date time format yyyy-MM-ddTHH:mm:ssZ

References

  1. ISO 8601 - Wikipedia
  2. ISO week date - Wikipedia
  3. DateTime.ParseExact Method - Microsoft Docs
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.