Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Cort Larsen 421 posts 1038 karma points
    Oct 03, 2012 @ 22:10
    Peter Cort Larsen
    0

    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.

    <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>  
  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 04, 2012 @ 10:04
    Dan Diplo
    0

    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>
    
    I haven't tested this extensively, but should point you in the right direction...

     

  • Peter Cort Larsen 421 posts 1038 karma points
    Oct 04, 2012 @ 10:38
    Peter Cort Larsen
    0

    Hi,

    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>
Please Sign in or register to post replies

Write your reply to:

Draft