Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi,
I have some nodes which is events, they have a "StartDate" property of type Date Picker.
I would like to loop over the dates in these nodes, and add the Years and moth to a select list like shown below.
<select><optgroup label="2012"> <option value="2012.1">Jan</option> <option value="2012.3">Mar</option> </optgroup> <optgroup label="2011"> <option value="2011.10">Oct</option> <option value="2011.12">Dec</option> </optgroup></select>
This is the code i have so far. I cant find a way to filter years and months.
<select name="bydate" id="bydate"> <option value="0">@Dictionary["FilterByDate"]</option> @foreach (var item in @Model.Descendants().Where("nodeTypeAlias == \"Events\"")) { var aYear = umbraco.library.FormatDateTime(item.GetPropertyValue("StartDate"), "yyyy"); var aMonth = umbraco.library.FormatDateTime(item.GetPropertyValue("StartDate"), "mm"); @*<optgroup label="2012"> <option value="2012.1">Jan</option> <option value="2012.3">Mar</option> </optgroup> <optgroup label="2011"> <option value="2011.10">Oct</option> <option value="2011.12">Dec</option> </optgroup>*@ } </select>
So basically you need to generate a list of all distinct years and within each year all distinct months for that year?
First, you need to generate a list of all events ordered by StartDate. Simplest way to do this is:
var orderedEvents = Model.Descendants("Events").OrderBy("StartDate");
You can then generate your select list, with options, via a loop something like this:
int lastYear = 0; int lastMonth = 0; <select> @foreach (var e in orderedEvents) { int year = e.StartDate.Year; int month = e.StartDate.Month; if (year > lastYear) { @:<optgroup label="@year"> } if (year != lastYear || month > lastMonth) { <option value="@year.@month"> @e.StartDate.ToString("MMM") </option> } lastYear = year; lastMonth = month; if (year > lastYear) { @:</optgroup> } } </select>
Thanks, with a little tweak it works perfect.
var currentYear = DateTime.Now.Year; int lastYear = 0; int lastMonth = 0; <select name="bydate" id="bydate"> <option value="0">@Dictionary["FilterByDate"]</option> @foreach (var e in @Model.Descendants().Where("nodeTypeAlias == \"Events\"").OrderBy("StartDate")) { int year = e.StartDate.Year; int month = e.StartDate.Month; if (year >= currentYear) { if (year > lastYear) { @:<optgroup label="@year"> } if (year != lastYear || month > lastMonth) { <option value="@year.@month"> @e.StartDate.ToString("MMM")</option> } lastYear = year; lastMonth = month; if (year > lastYear) { @:</optgroup> } } } </select>
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
foreach dates
Hi,
I have some nodes which is events, they have a "StartDate" property of type Date Picker.
I would like to loop over the dates in these nodes, and add the Years and moth to a select list like shown below.
This is the code i have so far. I cant find a way to filter years and months.
So basically you need to generate a list of all distinct years and within each year all distinct months for that year?
First, you need to generate a list of all events ordered by StartDate. Simplest way to do this is:
You can then generate your select list, with options, via a loop something like this:
Hi,
Thanks, with a little tweak it works perfect.
is working on a reply...