Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Apr 22, 2016 @ 18:51
    Paul Griffiths
    0

    Razor/LINQ to compare date picker with time value with current dateTime.Now

    Hi All,

    I am struggling to filter events by 'end date' and wondering if anyone can please help?

    I have a parent node called events (doc type eventPage) and from events you can create child pages( of doctype type event). On the event doc type i have defined a date picker with time like so

    enter image description here

    On the event page i only want to display a list events that have an end date which hasnt passed the current date and time. I step up to the parent get a list of child nodes and need to compare the end date with the current date

    Attepting suggestions made in this post https://our.umbraco.org/forum/developers/razor/26022-Razor-Where-DatetTime-Greater-Than-Or-Equal-DateX- so far i havent been able to achieve this filter.

     var currentDate = DateTime.Now; 
        var events = Model.Content.Parent.Children.OrderBy("startDate desc").Where("endDate >= @0", currentDate);
    

    Has anyone cracked this or can see where i am going wrong?

    Thanks Paul

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 22, 2016 @ 19:18
    Dan Diplo
    100

    Would something like this work?

    var events = Model.Content.Parent.Children.
        Where(x => x.GetPropertyValue<DateTime>("endDate").Date <= DateTime.Today).
        OrderByDescending(x => x.GetPropertyValue<DateTime>("startDate"));
    
  • Paul Griffiths 370 posts 1021 karma points
    Apr 22, 2016 @ 19:28
    Paul Griffiths
    0

    Hi Dan,

    Ive been at this hours and youve just solved it for me :). Looks so easy when the code is in front of me but been pulling my hair our.

    I made one slight amend on the check (<=). It looks like this

    var events = Model.Content.Parent.Children.
        Where(x => x.GetPropertyValue<DateTime>("endDate").Date >= DateTime.Today).
        OrderByDescending(x => x.GetPropertyValue<DateTime>("startDate"));
    

    Cant thank you enough mate

    Paul

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 22, 2016 @ 19:33
    Dan Diplo
    1

    Hey, glad to have helped Paul! Though as you see, I always get the less-than more-than operators mixed up when dealing with dates :)

  • Paul Griffiths 370 posts 1021 karma points
    Apr 22, 2016 @ 19:57
    Paul Griffiths
    0

    Hi Dan,

    No problems as long as its sorted is the main thing and without your help id be still struggling :)

    Paul

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies