DateTime in ISO 8601 Format
ISO 8601 date and time format is something I find the best format in logs and file names but can never keep the magic format string in my head. So, I wrote a couple of extension methods, one for standard and one for extended format.
[Test]
[TestCase(new DateTime(2015, 1, 7, 11, 19, 8), "2015-01-07 111908")]
public void ISO8601Format_Dates_Are_Ordered_Year_First(DateTime date, string formatted)
{
Assert.AreEqual(date.ToISO8601(), formatted);
}
[Test]
[TestCase(new DateTime(2015, 1, 7, 11, 19, 8), "2015-01-07 11:19:08")]
public void ISO8601ExFormat_Dates_Are_Ordered_Year_First_With_Time_Colons(DateTime date, string formatted)
{
Assert.AreEqual(date.ToISO8601Ex(), formatted);
}
public static string ToISO8601(this DateTime dt)
{
return dt.ToString("yyyy-MM-dd hhmmss");
}
public static string ToISO8601Ex(this DateTime dt)
{
return dt.ToString("yyyy-MM-dd hh:mm:ss");
}