Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace ConvertDateTimeToIso8601String
namespace ConvertDateTimeToIso8601String;

public static class DateTimeExtensions
{
public static class DateTimeExtensions
public static string ToUniversalIso8601(this DateTime dateTime)
{
return dateTime.ToUniversalTime().ToString("u").Replace(" ", "T");
}

public static string ToShortIso8601WeekDateString(this DateTime dateTime, bool useSeparator = true)
{
var separator = useSeparator ? "-" : string.Empty;

var weekYear = ISOWeek.GetYear(dateTime);
var weekOfYear = ISOWeek.GetWeekOfYear(dateTime);
var weekPlaceHolder = $"{weekOfYear}".PadLeft(2, '0');

return $"{weekYear}{separator}W{weekPlaceHolder}";
}

public static string ToExtendedIso8601WeekDateString(this DateTime dateTime, bool useSeparator = true)
{
public static string ToUniversalIso8601(this DateTime dateTime)
{
return dateTime.ToUniversalTime().ToString("u").Replace(" ", "T");
}

public static string ToShortIso8601WeekDateString(this DateTime dateTime, bool useSeparator = true)
{
var separator = useSeparator ? "-" : string.Empty;
var culture = CultureInfo.InvariantCulture;
var format = culture.DateTimeFormat;

var weekOfYear = culture.Calendar.GetWeekOfYear(dateTime, format.CalendarWeekRule, format.FirstDayOfWeek);
var weekPlaceHolder = $"{weekOfYear}".PadLeft(2, '0');

return $"{dateTime.Year}{separator}W{weekPlaceHolder}";
}

public static string ToExtendedIso8601WeekDateString(this DateTime dateTime, bool useSeparator = true)
{
var separator = useSeparator ? "-" : string.Empty;

return $"{dateTime.ToShortIso8601WeekDateString(useSeparator)}{separator}{(int)dateTime.DayOfWeek}";
}

public static string ToIso8601OrdinalDateString(this DateTime dateTime, bool useSeparator = true)
{
var separator = useSeparator ? "-" : string.Empty;
var dayPlaceHolder = $"{dateTime.DayOfYear}".PadLeft(3, '0');

return $"{dateTime.Year}{separator}{dayPlaceHolder}";
}
var separator = useSeparator ? "-" : string.Empty;
var dayOfWeek = dateTime.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)dateTime.DayOfWeek;

return $"{dateTime.ToShortIso8601WeekDateString(useSeparator)}{separator}{dayOfWeek}";
}

public static string ToIso8601OrdinalDateString(this DateTime dateTime, bool useSeparator = true)
{
var separator = useSeparator ? "-" : string.Empty;
var dayPlaceHolder = $"{dateTime.DayOfYear}".PadLeft(3, '0');

return $"{dateTime.Year}{separator}{dayPlaceHolder}";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Globalization;
using ConvertDateTimeToIso8601String;

// The comments show the output on a machine whose time zone is UTC+06:00.
var localTime = new DateTime(2022, 1, 13, 16, 25, 35, 125, DateTimeKind.Local);
var utcTime = new DateTime(2022, 1, 13, 16, 25, 35, 125, DateTimeKind.Utc);

Expand All @@ -11,53 +13,77 @@
Console.WriteLine($"Local: {localTime.ToString("o")}"); // 2022-01-13T16:25:35.1250000+06:00
Console.WriteLine($"UTC: {utcTime.ToString("o")}"); // 2022-01-13T16:25:35.1250000Z

// Round-trip format with a DateTimeOffset
var offsetTime = new DateTimeOffset(2022, 1, 13, 16, 25, 35, 125, TimeSpan.FromHours(6));

Console.WriteLine(offsetTime.ToString("o")); // 2022-01-13T16:25:35.1250000+06:00
Console.WriteLine(offsetTime.ToUniversalTime().ToString("o")); // 2022-01-13T10:25:35.1250000+00:00

// Universal datetime format specifier
Console.WriteLine($"Local: {localTime.ToUniversalIso8601()}"); // 2022-01-13T10:25:35Z
Console.WriteLine($"UTC: {utcTime.ToUniversalIso8601()}"); // 2022-01-13T16:25:35Z

// Basic syntax for custom formats
var output = localTime.ToString("yyyy-MM-ddTHH:mm:ssK");
var output = localTime.ToString("yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture);

// Custom formats follow the current culture unless we pass one
CultureInfo.CurrentCulture = new CultureInfo("fi-FI");
Console.WriteLine(localTime.ToString("yyyy-MM-ddTHH:mm:ssK")); // 2022-01-13T16.25.35+06:00
Console.WriteLine(localTime.ToString("yyyy-MM-ddTHH:mm:ssK", CultureInfo.InvariantCulture)); // 2022-01-13T16:25:35+06:00

CultureInfo.CurrentCulture = new CultureInfo("th-TH");
Console.WriteLine(localTime.ToString("yyyy-MM-dd")); // 2565-01-13
Console.WriteLine(localTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)); // 2022-01-13

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

// ISO 8601 compliant custom formats
var customFormats = new[]
{
"yyyy-MM-dd",
"yyyyMMdd",
"yyyy-MM",
"--MM-dd",
"--MMdd",
"THH:mm:ss.fff",
"THH:mm:ss",
"THH:mm",
"THH",
"THHmmssfff",
"yyyyMMddTHHmmssK",
"THHmmss.fff",
"yyyy-MM-ddTHH:mm:ssK",
"yyyyMMddTHHmm",
"yyyy-MM-ddTHH:mmZ",
"yyyyMMddTHHmmsszzz",
"yyyy-MM-ddTHH:mm:sszzz",
"yyyyMMddTHHmmsszz",
};

customFormats.ToList().ForEach(f => Console.WriteLine($"{f} \t {localTime.ToString(f)}"));
foreach (var customFormat in customFormats)
{
Console.WriteLine($"{customFormat} \t {localTime.ToString(customFormat, CultureInfo.InvariantCulture)}");
}

// 'Z' is a literal, so it belongs on UTC values only
Console.WriteLine(utcTime.ToString("yyyy-MM-ddTHH:mmZ", CultureInfo.InvariantCulture)); // 2022-01-13T16:25Z

var format = "yyyy-MM-ddTHH:mm:ssK";
var unspecified = new DateTime(2022, 1, 13, 16, 25, 30, DateTimeKind.Unspecified);
var utc = new DateTime(2022, 1, 13, 16, 25, 30, DateTimeKind.Utc);
var local = new DateTime(2022, 1, 13, 16, 25, 30, DateTimeKind.Local);

Console.WriteLine(unspecified.ToString(format)); // 2022-01-13T16:25:30
Console.WriteLine(utc.ToString(format)); // 2022-01-13T16:25:30Z
Console.WriteLine(local.ToString(format)); // 2022-01-13T16:25:30+06:00
Console.WriteLine(local.ToString("yyyy-MM-ddTHH:mm:sszz")); // 2022-01-13T16:25:30+06
Console.WriteLine(unspecified.ToString(format, CultureInfo.InvariantCulture)); // 2022-01-13T16:25:30
Console.WriteLine(utc.ToString(format, CultureInfo.InvariantCulture)); // 2022-01-13T16:25:30Z
Console.WriteLine(local.ToString(format, CultureInfo.InvariantCulture)); // 2022-01-13T16:25:30+06:00
Console.WriteLine(local.ToString("yyyy-MM-ddTHH:mm:sszz", CultureInfo.InvariantCulture)); // 2022-01-13T16:25:30+06

// Week dates - short form
Console.WriteLine(localTime.ToShortIso8601WeekDateString()); // 2022-W03
Console.WriteLine(localTime.ToShortIso8601WeekDateString(useSeparator: false)); // 2022W03
Console.WriteLine(localTime.ToShortIso8601WeekDateString()); // 2022-W02
Console.WriteLine(localTime.ToShortIso8601WeekDateString(useSeparator: false)); // 2022W02

// Week dates - extended form
Console.WriteLine(localTime.ToExtendedIso8601WeekDateString()); // 2022-W03-4
Console.WriteLine(localTime.ToExtendedIso8601WeekDateString(useSeparator: false)); // 2022W034
Console.WriteLine(localTime.ToExtendedIso8601WeekDateString()); // 2022-W02-4
Console.WriteLine(localTime.ToExtendedIso8601WeekDateString(useSeparator: false)); // 2022W024

// Week dates near the turn of the year
Console.WriteLine(new DateTime(2021, 1, 1).ToExtendedIso8601WeekDateString()); // 2020-W53-5
Console.WriteLine(new DateTime(2022, 1, 2).ToExtendedIso8601WeekDateString()); // 2021-W52-7

// Ordinal dates
Console.WriteLine(localTime.ToIso8601OrdinalDateString()); // 2022-013
Console.WriteLine(localTime.ToIso8601OrdinalDateString(useSeparator: false)); // 2022013
Console.WriteLine(localTime.ToIso8601OrdinalDateString(useSeparator: false)); // 2022013
Loading
Loading