EN
C# / .NET - convert DateTime to iso 8601
14 points
In this article, we are going to show how in C# / .NET convert DateTime
object to iso 8601 string.
Quick solution:
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
DateTime time = DateTime.Now;
8
DateTime utcTime = DateTime.UtcNow;
9
10
// when we want local time with offset
11
Console.WriteLine(time.ToString("yyyy-MM-ddTHH:mm:sszzz")); // 2020-07-28T08:40:39+00:00
12
13
// when we want world global time
14
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ssZ")); // 2020-07-28T08:40:39Z
15
}
16
}
Output:
xxxxxxxxxx
1
2020-07-28T08:40:39+00:00
2
2020-07-28T08:40:39Z
Look to the below to see full examples:
It is importnant to use invariant culture info to get proper formatted time from ToString()
method.
xxxxxxxxxx
1
using System;
2
using System.Globalization;
3
4
public class Program
5
{
6
private static CultureInfo culture = CultureInfo.InvariantCulture;
7
8
public static void Main()
9
{
10
DateTime time = DateTime.Now;
11
DateTime utcTime = DateTime.UtcNow;
12
13
// without time zone infomration
14
Console.WriteLine(time.ToString("s", culture)); // 2020-05-22T21:08:42
15
Console.WriteLine(utcTime.ToString("s", culture)); // 2020-05-22T21:08:42
16
17
// with time zone information (+/-HH:MM or Z)
18
Console.WriteLine(time.ToString("o", culture)); // 2020-05-22T21:08:42.9082887+00:00
19
Console.WriteLine(utcTime.ToString("o", culture)); // 2020-05-22T21:08:42.9082887Z
20
}
21
}
Output:
xxxxxxxxxx
1
2020-05-22T21:08:42
2
2020-05-22T21:08:42
3
2020-05-22T21:08:42.9082887+00:00
4
2020-05-22T21:08:42.9082887Z
There are different formats of iso 8601 times.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
public static void Main()
6
{
7
DateTime time = DateTime.Now;
8
9
Console.WriteLine(time.ToString("yyyy-MM-ddTHH:mm:sszzz")); // 2019-08-18T07:36:13+01:00
10
Console.WriteLine(time.ToString("yyyyMMdd")); // 20190818
11
Console.WriteLine(time.ToString("--MM-dd")); // --08-18
12
13
DateTime utcTime = DateTime.UtcNow;
14
15
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ssZ")); // 2019-08-18T06:36:13Z
16
Console.WriteLine(utcTime.ToString("yyyyMMddTHHZ")); // 20190818T06Z
17
}
18
}
19
20
// ------------------------------------------------------
21
// Check other custom formats at the end of this section.
22
//
Output:
xxxxxxxxxx
1
2019-08-18T07:36:13+01:00
2
2019-08-18T06:36:13Z
3
20190818T06Z
4
20190818
5
--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:
xxxxxxxxxx
1
/*
2
3
--[ To use with DateTime.Now ]----------------------
4
FORMAT TEXT
5
6
yyyy-MM-ddTHH:mm:sszzz 2019-08-18T07:36:13+01:00
7
yyyy-MM-ddTHH:mm:sszz 2019-08-18T07:36:13+01
8
yyyy-MM-ddTHH:mm:ss 2019-08-18T07:36:13
9
yyyy-MM-ddTHH:mmzzz 2019-08-18T07:36+01:00
10
yyyy-MM-ddTHH:mmzz 2019-08-18T07:36+01
11
yyyy-MM-ddTHH:mm 2019-08-18T07:36
12
yyyy-MM-ddTHHzzz 2019-08-18T07+01:00
13
yyyy-MM-ddTHHzz 2019-08-18T07+01
14
yyyy-MM-ddTHH 2019-08-18T07
15
yyyy-MM-dd 2019-08-18
16
17
yyyyMMddTHHmmsszzz 20190818T073613+01:00
18
yyyyMMddTHHmmsszz 20190818T073613+01
19
yyyyMMddTHHmmzzz 20190818T0736+01:00
20
yyyyMMddTHHmmzz 20190818T0736+01
21
yyyyMMddTHHzzz 20190818T07+01:00
22
yyyyMMddTHHzz 20190818T07+01
23
yyyyMMddTHH 20190818T07
24
yyyyMMdd 20190818
25
26
--MM-dd --08-18
27
28
29
--[ To use with DateTime.UtcNow ]-------------------
30
FORMAT TEXT
31
32
yyyy-MM-ddTHH:mm:ssZ 2019-08-18T06:36:13Z
33
yyyy-MM-ddTHH:mmZ 2019-08-18T06:36Z
34
yyyy-MM-ddTHHZ 2019-08-18T06Z
35
36
yyyyMMddTHHmmssZ 20190818T063613Z
37
yyyyMMddTHHmmZ 20190818T0636Z
38
yyyyMMddTHHZ 20190818T06Z
39
40
*/
In this section, custom method that returns a week or ordinal date is presented.
xxxxxxxxxx
1
using System;
2
using System.Globalization;
3
4
public static class ISO8601TimeUtils
5
{
6
public static string GetSimpleWeekDate(DateTime time)
7
{
8
CultureInfo culture = CultureInfo.InvariantCulture;
9
10
Calendar calendar = culture.Calendar;
11
DateTimeFormatInfo format = culture.DateTimeFormat;
12
13
int week = calendar.GetWeekOfYear(time, format.CalendarWeekRule, format.FirstDayOfWeek);
14
15
return time.Year + "-W" + week;
16
}
17
18
public static string GetExtendedWeekDate(DateTime time)
19
{
20
string week = GetSimpleWeekDate(time);
21
22
return week + "-" + (int)time.DayOfWeek;
23
}
24
25
public static string GetOrdinalDate(DateTime time)
26
{
27
return time.Year + "-" + time.DayOfYear;
28
}
29
}
30
31
// Usage example:
32
33
public class Program
34
{
35
public static void Main()
36
{
37
DateTime time = DateTime.Now;
38
39
Console.WriteLine(ISO8601TimeUtils.GetExtendedWeekDate(time)); // 2019-W34-0
40
Console.WriteLine(ISO8601TimeUtils.GetSimpleWeekDate(time)); // 2019-W34
41
Console.WriteLine(ISO8601TimeUtils.GetOrdinalDate(time)); // 2019-230
42
}
43
}
Output:
xxxxxxxxxx
1
2019-W34-0
2
2019-W34
3
2019-230
Note: above example returns different week number depending of Calendar.GetWeekOfYear()
method configuration.