How do I convert a DateTime structure to its equivalent RFC 822 date-time formatted string representation and parse this string representation back to a DateTime structure in .NET? The RFC-822 date-time format is used in a number of specifications such as the RSS Syndication Format.
To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.
The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.
The strftime() method takes one or more format codes as an argument and returns a formatted string based on it. We imported datetime class from the datetime module. It's because the object of datetime class can access strftime() method. The datetime object containing current date and time is stored in now variable.
Datetime data types are used to store date and time information. The DATE data type consists of year, month, and day information to represent a date value. The default format for the DATE data type is YYYY-MM-DD. YYYY represents the year, MM represents the month, and DD represents the day.
Try this:
DateTime today = DateTime.Now; String rfc822 = today.ToString("r"); Console.WriteLine("RFC-822 date: {0}", rfc822); DateTime parsedRFC822 = DateTime.Parse(rfc822); Console.WriteLine("Date: {0}", parsedRFC822);
The "r" format specifier passed into DateTime's ToString() method actually yields an RFC-1123-formatted datetime string, but passes as an RFC-822 date as well, based on reading the specification found at http://www.w3.org/Protocols/rfc822/#z28. I've used this method in creating RSS feeds, and they pass validation based on the validator available at http://validator.w3.org/feed/check.cgi.
The downside is that, in the conversion, it converts the datetime to GMT. To convert back to local time you would need to apply your local timezone offset. For that, you might use the TimeZone class to get your current timezone offset, and replace "GMT" with a timezone offset string:
TimeZone tz = TimeZone.CurrentTimeZone; String offset = tz.GetUtcOffset().ToString(); // My locale is Mountain time; offset is set to "-07:00:00" // if local time is behind utc time, offset should start with "-". // otherwise, add a plus sign to the beginning of the string. if (!offset.StartsWith("-")) offset = "+" + offset; // Add a (+) if it's a UTC+ timezone offset = offset.Substring(0,6); // only want the first 6 chars. offset = offset.Replace(":", ""); // remove colons. // offset now looks something like "-0700". rfc822 = rfc822.Replace("GMT", offset); // The rfc822 string can now be parsed back to a DateTime object, // with the local time accounted for. DateTime new = DateTime.Parse(rfc822);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With