Created
October 9, 2014 09:32
-
-
Save sixeyed/a2d343d992125fe457ef to your computer and use it in GitHub Desktop.
C# extension method to get HTTP formatted date-time string from DateTimeOffset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.