Copied to clipboard

Flag this post as spam?

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


  • David Hyslop 27 posts 181 karma points
    Mar 08, 2017 @ 12:40
    David Hyslop
    0

    Only show items from specified year

    Hi

    Struggling with some code, wonder if anyone knows how to write this.

    I've got a collection of articles that have an end date specified on them. and I'm trying to only show items from a certain year.

    @{
        var selection = Umbraco.Content(3237).Children.Where("newsEndDate == 2016");
    }
    
    @foreach (var item in selection)
    {
    
    
        <div>@item.Name</div>
    
    }
    

    The above code doesn't work, I've also tried

     @{
        var selection = Umbraco.Content(3237).Children.Where("newsEndDate.ToString("yyyy") == 2017);
    }
    

    But still no luck, any help would be greatly appreciated.

    Thanks.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Mar 08, 2017 @ 13:35
    Alex Skrypnyk
    100

    Hi David

    Don't recommend you to use Dynamics in rendering of Umbraco data, try to use TypedContent instead of Content, this should work as you want:

    var selection = Umbraco.TypedContent(3237).Children.Where(node => node.HasValue("newsEndDate") && node.GetPropertyValue<DateTime>("newsEndDate").Year == 2017);
    

    Hope it will help.

    Thanks,

    Alex

  • David Hyslop 27 posts 181 karma points
    Mar 08, 2017 @ 14:22
    David Hyslop
    0

    Thanks Alex, that's worked perfect.

    Could I ask you one other quick question.

    So I've now got this

      @{
        var selection = Umbraco.TypedContent(3237).Children.Where(node => node.HasValue("newsEndDate") && node.GetPropertyValue<DateTime>("newsEndDate").Year == 2016);
    }
    
    @foreach (var item in selection)
    {
    
        <div class="date">  @item.newsEndDate.ToString("MMMM")   </div>
    
    }
    

    Since changing to Typed Content this no longer works @item.newsEndDate.ToString("MMMM") Is there another way I should be doing this now.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Mar 08, 2017 @ 14:25
    Alex Skrypnyk
    0

    You are welcome, David.

    Try this code:

    @foreach (var item in selection)
    {
        var dateTimeValue = item.GetPropertyValue<DateTime>("newsEndDate");
    
        <div class="date">@dateTimeValue.ToString("MMMM")</div>
    }
    
  • David Hyslop 27 posts 181 karma points
    Mar 08, 2017 @ 14:46
    David Hyslop
    0

    Spot on, thanks Alex

Please Sign in or register to post replies

Write your reply to:

Draft