How to use RFC1123 ("R", "r") Format Specifier in C#
Description
The "R" or "r" format is defined by the DateTimeFormatInfo.RFC1123Pattern property.
The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'".
The following the DateTimeFormatInfo properties controls the format.
- RFC1123Pattern
- AbbreviatedDayNames
- AbbreviatedMonthNames
The application must convert the date and time value to UTC before it performs the formatting operation. To perform this conversion, DateTime values can call the DateTime.ToUniversalTime method, and DateTimeOffset values can call the ToUniversalTime method.
Example
The following example uses the "r
" format
specifier to display a DateTime and a
DateTimeOffset
value on a system in the U.S. Pacific Time zone.
//from www .j av a2 s.c o m
using System;
class MainClass
{
public static void Main()
{
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
}
}
The code above generates the following result.