How do I convert a date to a HTTP-formatted date in .Net / C#

C#.NetHttp

C# Problem Overview


How does one convert a .Net DateTime into a valid HTTP-formatted date string?

C# Solutions


Solution 1 - C#

Dates can be converted to HTTP valid dates (RFC 1123) by using the "r" format string in .Net. HTTP dates need to be GMT / not offset - this can be done using the ToUniversalTime() method.

So, in C# for example:

string HttpDate = SomeDate.ToUniversalTime().ToString("r");

Right now, that produces HttpDate = "Sat, 16 Aug 2008 10:38:39 GMT"

See Standard Date and Time Format Strings for a list of .Net standard date & time format strings.

See Protocol Parameters for the HTTP date specification, and background to other valid (but dated) RFC types for HTTP dates.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGareth JenkinsView Question on Stackoverflow
Solution 1 - C#Gareth JenkinsView Answer on Stackoverflow