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?
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?
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. ;)
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:
This works. But it hit me that maybe Umbraco caches stuff? Will the todayDate variable reevaluated every time someone visits the site?
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?
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.
You can use the .Date property of the DateTime object. :)
https://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx
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:
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. ;)
Yup, that's way more readable. Thanks :)
is working on a reply...