Skip to content

Instantly share code, notes, and snippets.

@sixeyed
Created October 9, 2014 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixeyed/a2d343d992125fe457ef to your computer and use it in GitHub Desktop.
Save sixeyed/a2d343d992125fe457ef to your computer and use it in GitHub Desktop.
C# extension method to get HTTP formatted date-time string from DateTimeOffset
namespace System
{
/// <summary>
/// Extensions to <see cref="DateTimeOffset"/>
/// </summary>
public static class DateTimeOffsetExtensions
{
/// <summary>
/// Returns the localised date-time as GMT formatted for HTTP headers, e.g.
/// Thu, 09 Oct 2014 10:31:33 GMT
/// </summary>
public static string ToHttpHeaderString(this DateTimeOffset offset)
{
var gmt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(offset, "GMT Standard Time");
return string.Format("{0:ddd, dd MMM yyyy HH:mm:ss} GMT", gmt);
}
}
}
@mattjohnsonpint
Copy link

Uh... sorry, but no. "GMT Standard Time" is the time zone for London and the rest of the UK, which alternates between GMT (+00:00) and BST (+01:00) for daylight saving time. The correct time zone would just be "UTC".

However, this whole function can just be replaced by .ToString("R"). See The RFC1123 ("R", "r") Format Specifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment