tags:

views:

794

answers:

2

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

+14  A: 

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 http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for a list of .Net standard date & time format strings.

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html for the HTTP date specification, and background to other valid (but dated) RFC types for HTTP dates.

Gareth Jenkins
+2  A: 

To get the common web-formatted date of ISO 8601: as in: 2008-08-16T10:38:39Z. It's:

SomeDate.ToString("s");

or, as above for UTC:

SomeDate.ToUniversalTime().ToString("s");
jeffreypriebe