PL
C # / .NET - przekonwertuj DateTime na ISO 8601
3 points
W tym artykule pokażemy, jak w C # / .NET przekonwertować obiekt DateTime
na ciąg iso 8601.
Szybkie rozwiązanie:
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
Console.WriteLine(time.ToString("yyyy-MM-ddTHH:mm:sszzz")); // 2020-07-28T08:40:39+00:00
11
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mm:ssZ")); // 2020-07-28T08:40:39Z
12
}
13
}
Wynik:
xxxxxxxxxx
1
2020-07-28T08:40:39+00:00
2
2020-07-28T08:40:39Z
Poniżej znajdziesz pełne przykłady:
Ważne jest, aby użyć niezmiennych informacji o kulturze, aby uzyskać właściwy czas sformatowany z metody ToString()
.
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
}
Wynik:
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
Istnieją różne formaty czasów ISO 8601.
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
//
Wynik:
xxxxxxxxxx
1
2019-08-18T07:36:13+01:00
2
2019-08-18T06:36:13Z
3
20190818T06Z
4
20190818
5
--08-18
Uwaga: ciąg daty i godziny z przyrostkiem Z powinien być oparty na
DateTime.UtcNow
. Sprawdź ten link, aby uzyskać więcej informacji.
Inne formaty niestandardowe:
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
*/
W tej sekcji prezentowana jest niestandardowa metoda zwracająca tydzień lub datę porządkową.
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,
14
format.CalendarWeekRule, format.FirstDayOfWeek);
15
16
return time.Year + "-W" + week;
17
}
18
19
public static string GetExtendedWeekDate(DateTime time)
20
{
21
string week = GetSimpleWeekDate(time);
22
23
return week + "-" + (int)time.DayOfWeek;
24
}
25
26
public static string GetOrdinalDate(DateTime time)
27
{
28
return time.Year + "-" + time.DayOfYear;
29
}
30
}
31
32
// Usage example:
33
34
public class Program
35
{
36
public static void Main()
37
{
38
DateTime time = DateTime.Now;
39
40
Console.WriteLine(ISO8601TimeUtils.GetExtendedWeekDate(time)); // 2019-W34-0
41
Console.WriteLine(ISO8601TimeUtils.GetSimpleWeekDate(time)); // 2019-W34
42
Console.WriteLine(ISO8601TimeUtils.GetOrdinalDate(time)); // 2019-230
43
}
44
}
Wynik:
xxxxxxxxxx
1
2019-W34-0
2
2019-W34
3
2019-230
Uwaga: powyższy przykład zwraca inny numer tygodnia w zależności od konfiguracji metody
Calendar.GetWeekOfYear
.