In order to traverse all of your nodes, consider this traverse helper method that I use for creating a sitemap (you may want to remove maxLevelForSitemap):
@helper traverse(dynamic node){
var maxLevelForSitemap = 4;
var values = new Dictionary<string,object>();
values.Add("maxLevelForSitemap", maxLevelForSitemap) ;
var items = node.Children.Where("Visible && Level <= maxLevelForSitemap", values);
<ul>
@foreach (var item in items) {
<li><a href="@item.Url">@item.Name</a>
@traverse(item)
</li>
}
</ul>
}
Now in the @foreach you can do some date comparison (item.CreateDate is the one your probably want to compare against). This should output the result of "is the create date earlier than today":
@(item.CreateDate.Date < DateTime.Now.Date)
Make sure to use 4.7.1 for this to work properly by the way, I can't guarantee this will work in 4.7.0.
I actually just want to compare the difference between today's date and updated date to be less than 7.... so I tried as follows but it is giving razor error.
if(DateTime.Now.Date-item.UpdateDate.Date <= 7)
so what is the mistake in above line? .. is it like I have to format value to just date rather than datetime.. if so then how can I do that?
accessing all nodes
I am using umbraco 4.7 and new to this.
I want to loop through all nodes from the root node, access last updated date of node and compare it with today's date, how can I do that?
In order to traverse all of your nodes, consider this traverse helper method that I use for creating a sitemap (you may want to remove maxLevelForSitemap):
Ok... a bit helpfule for me. I changed the code as follows for 4.7.0 ...
<div>
@traverse(@Model.AncestorOrSelf())
</div>
@helper traverse(dynamic node){
var maxLevelForSitemap = 4;
var items = node.Children.Where("Visible");
<ul>
@foreach (var item in items)
{
if(DateTime.Now.Date>item.UpdateDate.Date)
{
<li><a href="@item.Url">@item.Name</a>
@traverse(item)
</li>
}
}
</ul>
}
I actually just want to compare the difference between today's date and updated date to be less than 7.... so I tried as follows but it is giving razor error.
if(DateTime.Now.Date-item.UpdateDate.Date <= 7)
so what is the mistake in above line? .. is it like I have to format value to just date rather than datetime.. if so then how can I do that?
You're subtracting dates, so you're getting a full date back, which can't be compared to the int 7. Try this:
Beware though, that you are stopping traversal of the children of items that are older than 7 days:
- Home (3 days old)
- Page 1 (8 days old)
- Page 2 (2 days old)
- Page 3 (4 days old)
- Page 4 (3 days old)
In this case, Page 1 is too old, so Page 2 will never be shown.
So you may want to do something like (this is completely untested):
yes.. thats what I want..
is working on a reply...