Copied to clipboard

Flag this post as spam?

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


  • Johan 188 posts 380 karma points
    Aug 12, 2015 @ 09:37
    Johan
    0

    Showing banners between dates

    I have made a banner system where you can pick banners from the media section. On the "Banner" media type i have Diplos date range picker where you can set the date when the banner will be shown. This is how i display the banners with a partial view:

    var todayDate = DateTime.Parse(DateTime.Now.ToShortDateString());
    var fishBannersCollection = Umbraco.TypedMedia(fishBannersList)
        .Where(x => x != null
            && DateTime.Compare(todayDate, DateTime.Parse(x.GetPropertyValue<Diplo.DateRangePicker.DateRange>("dateRange").EndDate.ToShortDateString())) <= 0
            && DateTime.Compare(todayDate, DateTime.Parse(x.GetPropertyValue<Diplo.DateRangePicker.DateRange>("dateRange").StartDate.ToShortDateString())) >= 0
        ).Take(numberOfItems).OrderBy(x => r.Next());
    

    This works. But it hit me that maybe Umbraco caches stuff? Will the todayDate variable reevaluated every time someone visits the site?

  • Lars-Erik Aabech 350 posts 1102 karma points MVP 8x c-trib
    Aug 12, 2015 @ 10:24
    Lars-Erik Aabech
    100

    Unless you use asp.net cache, that code will always run. It's the x.GetPropertyValue() values that comes from Umbraco's cache. It however, will be refreshed every time the content is published, so no worries!

    On a sidenote, I don't get why you .ToString() and .Parse() all the dates, when they are DateTime objects in the first place?

  • Johan 188 posts 380 karma points
    Aug 12, 2015 @ 10:43
    Johan
    0

    Thanks!

    I use ToShortDateString cause i just want the date, not the time. Looks pretty weird but it works. Maybe there are smarter ways to do it.

  • Lars-Erik Aabech 350 posts 1102 karma points MVP 8x c-trib
    Aug 12, 2015 @ 11:02
    Lars-Erik Aabech
    1

    You can use the .Date property of the DateTime object. :)
    https://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx

    DateTime.Now.Date == DateTime.Today
    
    x.GetPropertyValue<DateRange>("dateRange").EndDate.Date
    

    I'd be surprised if the latter is necessary, though. It should already be 00:00:00.

    So if I may be so frank, I'd rewrite your code to this:

    var fishBannersCollection = Umbraco.TypedMedia(fishBannersList)
        .Where(x => 
            x != null
            && DateTime.Today <= x.GetPropertyValue<Diplo.DateRangePicker.DateRange>("dateRange").EndDate
            && DateTime.Today >= x.GetPropertyValue<Diplo.DateRangePicker.DateRange>("dateRange").StartDate
        ).Take(numberOfItems).OrderBy(x => r.Next());
    

    You could also look into just making an extension method for DateTime that checks whether it's within a Diplo.DateRangePicker.DateRange. That would make the entire code piece beautiful, instead of difficult to read. ;)

  • Johan 188 posts 380 karma points
    Aug 12, 2015 @ 12:28
    Johan
    0

    Yup, that's way more readable. Thanks :)

Please Sign in or register to post replies

Write your reply to:

Draft