Languages
[Edit]
PL

C # / .NET - przekonwertuj DateTime na ISO 8601

3 points
Created by:
Sylwia
1334

W tym artykule poka┼╝emy, jak w C # / .NET przekonwertowa─ç obiekt DateTime na ci─ůg iso 8601.

Szybkie rozwi─ůzanie:

using System;

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

Wynik:

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

Poniżej znajdziesz pełne przykłady:

1. Przykład standardowego formatu daty i godziny

Wa┼╝ne jest, aby u┼╝y─ç niezmiennych informacji o kulturze, aby uzyska─ç w┼éa┼Ťciwy czas sformatowany z metody ToString().

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
	}
}

Wynik:

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.┬áPrzyk┼éad niestandardowych ci─ůg├│w formatu daty i godziny

Istniej─ů r├│┼╝ne formaty czas├│w ISO 8601.

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.
//

Wynik:

2019-08-18T07:36:13+01:00
2019-08-18T06:36:13Z
20190818T06Z
20190818
--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:

/*

--[ 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.┬áPrzyk┼éad niestandardowego tygodnia i daty porz─ůdkowej

W tej sekcji prezentowana jest niestandardowa metoda zwracaj─ůca tydzie┼ä lub dat─Ö porz─ůdkow─ů.

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
	}
}

Wynik:

2019-W34-0
2019-W34
2019-230

Uwaga: powy┼╝szy przyk┼éad zwraca inny numer tygodnia w zale┼╝no┼Ťci od konfiguracji metody Calendar.GetWeekOfYear.

Zobacz r├│wnie┼╝:

  1.  ISO 8601 - Co to jest Z w formacie daty i godziny rrrr-MM-ddTHH: mm: ssZ

Bibliografia:

  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
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.