ive got 2 strings, date:"27.03.11 " and time:"15:04", which id like to format as a PubDate elemnt for a rss file like Fri, 18 Nov 2005 19:12:30 GMT. How can i do this in c sharp?
Asked
Active
Viewed 1,823 times
1 Answers
2
Use the following steps:
- Parse the date and time strings into one
DateTime
variable. Use the DateTime.ParseExact static method for this. - Convert the datetime to GMT using the methods of the TimeZone class (if desired---I don't think this is mandatory according to the RSS specification).
Format this variable into a string using the DateTime.ToString method. The following MSDN pages will help you choose the correct format string based on your needs:
Since RSS requires dates to be in the RFC 822 format, the following related SO question might help you with the last step:
EDIT: For the first step, have a look at this example:
var s = "27.03.11 15:04";
var dtm = DateTime.ParseExact(s, @"dd.MM.yy HH\:mm", null);
(The \:
ensures that :
is seen as a literal :
rather than a culture-specific time separator.)
-
Can u help me withe the first part? Itried the following example from a tutorial but always get the error "String was not recognized as a valid DateTime." String MyString; MyString = "1999-09-01 21:34 PM"; //MyString = "1999-09-01 21:34 p.m."; //Depends on your regional settings DateTime MyDateTime; MyDateTime = new DateTime(); MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null); – kojoma May 12 '11 at 16:22
-
That date doesn't make sense: If it's 24hour time, you don't need am or pm. I've added an example for the first part to my answer. – Heinzi May 13 '11 at 10:02
-
@kojoma: PS: Don't trust that tutorial, it's bad code (for example, there's no need to set MyDateTime to "new DateTime()" before filling it with ParseExact). – Heinzi May 13 '11 at 10:05