如何在 .Net/C# 中将日期转换为 HTTP 格式的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:45:14  来源:igfitidea点击:

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

提问by Gareth Jenkins

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

如何将 .Net DateTime 转换为有效的 HTTP 格式的日期字符串?

回答by Gareth Jenkins

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.

通过使用 .Net 中的“r”格式字符串,可以将日期转换为 HTTP 有效日期 (RFC 1123)。HTTP 日期需要是 GMT / 不是偏移量 - 这可以使用 ToUniversalTime() 方法来完成。

So, in C# for example:

因此,以 C# 为例:

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

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

现在,产生 HttpDate = "Sat, 16 August 2008 10:38:39 GMT"

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

有关.Net 标准日期和时间格式字符串的列表,请参阅标准日期和时间格式字符串。

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

请参阅HTTP 日期规范的协议参数,以及 HTTP 日期的其他有效(但过时)RFC 类型的背景。