0

I have a date in String format,

 mm/dd/yyyy

I want to convert this to RSS date format like,

friday, june 01, 2012, 12:11:25PM

and i want to add this date value which is basically the pubDate to RSS pubDate tag, as follows:

writer.WriteElementString("pubDate",pubDate);

How can i convert this string to pubDate? any suggestions?

O/P

    <?xml version="1.0" encoding="utf-8"?><rss version="2.0">
<channel>
<title>About RSS</title>
<link>http://localhost:27549/TTTT.aspx</link>
<description>The latest news</description>
<image><url>http://localhost:27549/images/ttt_logo.jpg</url></image>
<item><title>ABC</title><link>http://localhost:27549/Viewttt.aspx?id=217</link><description>zzzzzzzzzzzzzzzzzzz...</description><pubDate>Tuesday, August 30, 2011, 00:00:00AM</pubDate></item></channel></rss>
Geek
  • 3,187
  • 15
  • 70
  • 115

2 Answers2

2

Fisrt you convert to DateTime variable. For this you can use DateTime.ParseExact.

Then you can use the ToString method to output the date in whatever format you want. Here is the list of custom formats

I will post an example to help you.

string str = "11/10/1984";
DateTime dt = DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.InvariantCulture);
string formatted = dt.ToString("dddd, MMMM dd, yyyy, HH:mm:sstt");
nunespascal
  • 17,584
  • 2
  • 43
  • 46
  • You will find it in `System.Globalization`. It says that the date culture is invariant. Invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. – nunespascal Jun 08 '12 at 04:07
  • I have an issue now, i get the date properly. ViewSource shows Tuesday, August 30, 2011, 00:00:00AM but I can see nothing in front end. What could that be? any idea? – Geek Jun 08 '12 at 04:13
  • If i just give writer.WriteElementString("pubDate", DateTime.Now.ToString("r")); i get the output ie todays date n timestamp... – Geek Jun 08 '12 at 04:22
  • I am not an expert on RSS, posting a seperate question with details of your RSS output would help. – nunespascal Jun 08 '12 at 04:26
1
var dt=DateTime.ParseExact("06/08/2012", "MM/dd/yyyy", CultureInfo.InvariantCulture)
writer.WriteElementString("pubDate",dt.ToString("U")); // or dt.ToString("F")
ebattulga
  • 10,774
  • 20
  • 78
  • 116