@{ //For each item node we can then ouput what we want foreach (XmlNode node in rssItems) { <li> <div class="date">@node["pubDate"].InnerText</div> <a href="@node["link"].InnerText">@node["title"].InnerText<span>></span></a> </li> } }
I was helping Anders out on Friday and helped him write a Razor snippet that outputs items from a remote XML source such as an RSS feed, so it's more custom .NET Razor code rather than Umbraco related. The RSS item has an XML Node called PubDate that uses a date format such as Thu, 17 Nov 2011 21:37:00 +0100
Going back to Anders' problem, I'm using ver similar code to grab a load of stuff from an external rss feed and then render on my site. Everything works fine, but the date is not in a nice format (standard RSS output).
I would like to get the solution Warren provided to work, but I'm not sure how to assign my date to the newstring variable that is highlighted in the example.
@foreach (DataRow entry in entries.Tables["item"].Rows) {
if (itemCounter < maxItems) {
So here I would like to switch out the literal string from the example and replace with my @entry["pubDate"] value.
how do I format datetime in razor?
I've searched and I've tried many combinations to no avail. The p#date displays
4/7/2011 12:00:00 AM
using my code below :
I would like to make it display just the dd/MM/yyyy format.
Cheers
Use this:
<p id="date">@article.ArticleDate.ToString("dd/MM/yyyy")</p>
Nice one. Thanks!
Your welcome... flag as solution to help others.
Don't forget: you can use anything that is in the C# language already. Basically, Razor is C# mixed with HTML and some @ signs.. :)
Thanks again, yes, I'm just starting in this, so appreciate the support.
Hi,
I got this I want to format with Razor - how to?
hmmmm i think with dot notation it should work. Have to look like this:
@node.link or @node.title
or test out
@node.GetProperty("link").value or @node.GetProperty("title").value
just test it out. Some of this should do the trick
Hi Anders,
You should use @node.Link, @node.Title etc. The first letter should always be capitalized, unless you want recursive, then you need to prefix with "_"
You can have a look at the the razor "cheat sheet" for more info : http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet
Cheers,
Michael.
Thanks for the replies.
I get the right data out, but I need to format it corret.
I would like to do something like this
Hi Anders,
I'm not 100% sure, but one of the following should work:
@node["pubDate"].Value.ToString("dd.MM.yyyy")
or
@node.PubDate.ToString("dd.MM.yyyy")
Cheers,
Michael.
I was helping Anders out on Friday and helped him write a Razor snippet that outputs items from a remote XML source such as an RSS feed, so it's more custom .NET Razor code rather than Umbraco related.
The RSS item has an XML Node called PubDate that uses a date format such as Thu, 17 Nov 2011 21:37:00 +0100
Anders to convert this into your own custom date time format you can use the following, which I found from this stackOverflow post
http://stackoverflow.com/questions/266448/format-rss-pubdate-as-net-datetime
string orig ="Wed, 29 Oct 2008 14:14:48 +0000";
string newstring =String.Format("{0:MM/dd/yyyy hh:mm tt}",DateTime.Parse(orig.Remove(orig.IndexOf(" +"))));
I hope this helps Anders.
Warren
Hey Warren,
Sorry but I still don't get it.
I think I'll go back to XSLT for now - i got a deadline on the site :(
Drop me a mail or hit me up on Skype Anders if you want and i'll give you a hand.
Warren
Hi, is there also a way to format a date in Razor like this:
"Wednesday 2 March 2012"
thanks for your help,
Anthony
date.ToString("dddd d MMMM yyyy");
@Daniel Bardi , works! Thanks a lot Daniel
greetings,
Anthony
Hi,
Any idea why this fails to load the razor script in Umbraco 4.7.0? I just want to output the current year in the copyright statement:-
Thanks
Craig
The answer:
@DateTime.Now.ToString("yyyy")
Craig
String to Datetime...
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
DateTime.Parse()
Going back to Anders' problem, I'm using ver similar code to grab a load of stuff from an external rss feed and then render on my site. Everything works fine, but the date is not in a nice format (standard RSS output).
I would like to get the solution Warren provided to work, but I'm not sure how to assign my date to the newstring variable that is highlighted in the example.
So here I would like to switch out the literal string from the example and replace with my @entry["pubDate"] value.
string orig = "Wed, 29 Oct 2008 14:14:48 +0000";
Psuedo code would be something like
string orig = @entry["pubDate"];
Then I could use the newstring value instead of entry["pubDate"].. Any help with syntax would be much appreciated. Razor hurts my brain..
Thanks,
Tom
Hi Tom,
Have you tried something like this, perhaps it works.
I haven't tested it myself, but hope this helps,
/Dennis
Tried that and variations on that, but no success.
Got it!
string myDate = (string)entry["pubDate"];
string newDate = String.Format("{0:ddd, dd MMMM yyyy}",DateTime.Parse(myDate.Remove(myDate.IndexOf(" +"))));
The field needed to be converted into a string for the variable to accept it.
Reference:
http://stackoverflow.com/questions/5168635/convert-datarow-to-int
is working on a reply...