ExtensionMethod.NET Home of 816 C#, Visual Basic, F# and Javascript extension methods

ToRFC822DateString

Converts a regular DateTime to a RFC822 date string used for RSS feeds

Source

/// <summary>
/// Converts a regular DateTime to a RFC822 date string.
/// </summary>
/// <returns>The specified date formatted as a RFC822 date string.</returns>
public static string ToRFC822DateString(this DateTime date) {
    int offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;
    string timeZone = "+" + offset.ToString().PadLeft(2, '0');
    if (offset < 0) {
        int i = offset * -1;
        timeZone = "-" + i.ToString().PadLeft(2, '0');
    }
    return date.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'), System.Globalization.CultureInfo.GetCultureInfo("en-US"));
}

Example

var s = DateTime.Now.ToRFC822DateString();

Author: unknown

Submitted on: 7 mrt 2008

Language: C#

Type: System.DateTime

Views: 3622