Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created April 5, 2011 16:43
Show Gist options
  • Save prabirshrestha/903972 to your computer and use it in GitHub Desktop.
Save prabirshrestha/903972 to your computer and use it in GitHub Desktop.
ISO8601 Date Time helpers
public static class DateTimeHelpers
{
private static readonly string[] Iso8601Format = new[]
{
"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'",
@"yyyy-MM-dd\THH:mm:ssK"
};
//SortableDateTimePattern (ISO 8601)
public static string ToIso8601(DateTime value)
{
return value.ToUniversalTime().ToString(Iso8601Format[0], CultureInfo.InvariantCulture);
}
public static DateTime ParseIso8601(string value)
{
return DateTime.ParseExact(value, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment