Languages
[Edit]
EN

C# / .NET - convert DateTime to iso 8601

14 points
Created by:
Trenton-McKinney
618

In this article, we are going to show how in C# / .NET convert DateTime object to iso 8601 string.

Quick solution:

using System;

public class Program
{
	public static void Main()
	{
		DateTime time = DateTime.Now;
		DateTime utcTime = DateTime.UtcNow;
		
		// when we want local time with offset
		Console.WriteLine(time.ToString("yyyy-MM-ddTHH:mm:sszzz"));   // 2020-07-28T08:40:39+00:00

		// when we want world global time
		Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));  // 2020-07-28T08:40:39Z
	}
}

Output:

2020-07-28T08:40:39+00:00
2020-07-28T08:40:39Z

 

Look to the below to see full examples:

1. Standard date and time format strings example

It is importnant to use invariant culture info to get proper formatted time from ToString() method.

using System;
using System.Globalization;

public class Program
{
	private static CultureInfo culture = CultureInfo.InvariantCulture;

	public static void Main()
	{
		DateTime time = DateTime.Now;
		DateTime utcTime = DateTime.UtcNow;
		
		// without time zone infomration
		Console.WriteLine(time.ToString("s", culture));    // 2020-05-22T21:08:42
		Console.WriteLine(utcTime.ToString("s", culture)); // 2020-05-22T21:08:42

		// with time zone information (+/-HH:MM or Z)
		Console.WriteLine(time.ToString("o", culture));    // 2020-05-22T21:08:42.9082887+00:00
		Console.WriteLine(utcTime.ToString("o", culture)); // 2020-05-22T21:08:42.9082887Z
	}
}

Output:

2020-05-22T21:08:42
2020-05-22T21:08:42
2020-05-22T21:08:42.9082887+00:00
2020-05-22T21:08:42.9082887Z

2. Custom date and time format strings example

There are different formats of iso 8601 times.

using System;

public class Program
{
	public static void Main()
	{
		DateTime time = DateTime.Now;

		Console.WriteLine(time.ToString("yyyy-MM-ddTHH:mm:sszzz"));  // 2019-08-18T07:36:13+01:00
		Console.WriteLine(time.ToString("yyyyMMdd"));                // 20190818
		Console.WriteLine(time.ToString("--MM-dd"));                 // --08-18

		DateTime utcTime = DateTime.UtcNow;

		Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ssZ")); // 2019-08-18T06:36:13Z
		Console.WriteLine(utcTime.ToString("yyyyMMddTHHZ"));         // 20190818T06Z
	}
}

// ------------------------------------------------------
// Check other custom formats at the end of this section.
//

Output:

2019-08-18T07:36:13+01:00
2019-08-18T06:36:13Z
20190818T06Z
20190818
--08-18
Note: date and time string with Z suffix should be based on DateTime.UtcNow. Check this link for more details.

Other custom formats:

/*

--[ To use with DateTime.Now ]----------------------
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


--[ To use with DateTime.UtcNow ]-------------------
FORMAT                    TEXT

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

*/

3. Custom week and ordinal date example

In this section, custom method that returns a week or ordinal date is presented.

using System;
using System.Globalization;

public static class ISO8601TimeUtils
{
	public static string GetSimpleWeekDate(DateTime time)
	{
		CultureInfo culture = CultureInfo.InvariantCulture;

		Calendar calendar = culture.Calendar;
		DateTimeFormatInfo format = culture.DateTimeFormat;

		int week = calendar.GetWeekOfYear(time, format.CalendarWeekRule, format.FirstDayOfWeek);

		return time.Year + "-W" + week;
	}

	public static string GetExtendedWeekDate(DateTime time)
	{
		string week = GetSimpleWeekDate(time);

		return week + "-" + (int)time.DayOfWeek;
	}

	public static string GetOrdinalDate(DateTime time)
	{
		return time.Year + "-" + time.DayOfYear;
	}
}

// Usage example:

public class Program
{
	public static void Main()
	{
		DateTime time = DateTime.Now;

		Console.WriteLine(ISO8601TimeUtils.GetExtendedWeekDate(time)); // 2019-W34-0
		Console.WriteLine(ISO8601TimeUtils.GetSimpleWeekDate(time));   // 2019-W34
		Console.WriteLine(ISO8601TimeUtils.GetOrdinalDate(time));      // 2019-230
	}
}

Output:

2019-W34-0
2019-W34
2019-230
Note: above example returns different week number depending of Calendar.GetWeekOfYear() method configuration.

 

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. Standard Date and Time Format Strings - Microsoft Docs
  4. Custom date and time format strings - Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - DateTime conversion

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join