Here is the actual final code I created, works fine for me (razor umbraco 7 - not MVC but easilly converted I suspect). It is a filter for date picker, so you can effectively have a calendar sorted/split by month.
@* Load dates for start and end of current month *@
DateTime StartDate1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
Date problems
Hi All,
Hope someone can help me. I have a bit of code where I am trying to put some date filtering in.
It is driving me potty.
This works:
foreach (dynamic eventItem in node.Descendants().Where("eventStart > DateTime.Now.AddMonths(1).AddDays(-1) && nodeTypeAlias.Contains(\"Event\")").OrderBy("eventStart")) {
int monnth = 1;
DateTime noww = DateTime.Now.AddMonths(monnth).AddDays(-1);
foreach (dynamic eventItem in node.Descendants().Where("eventStart > DateTime.Now.AddMonths(monnth).AddDays(-1) && nodeTypeAlias.Contains(\"Event\")").OrderBy("eventStart")) {
or
foreach (dynamic eventItem in node.Descendants().Where("eventStart > noww && nodeTypeAlias.Contains(\"Event\")").OrderBy("eventStart")) {
I am sure I am missing something super simple, but it has brought my project to a complete stand still.
Any help much appreciated.
Thanks
Stephen
Forgot to mention. Umbraco 7 using Razor (not MVC).
Hi Stephen
Try parsing in your parameter like this:
Thanks Allan,
Super easy when you know how.
Here is the actual final code I created, works fine for me (razor umbraco 7 - not MVC but easilly converted I suspect). It is a filter for date picker, so you can effectively have a calendar sorted/split by month.
@* Load dates for start and end of current month *@
DateTime StartDate1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime EndDate1 = StartDate1.AddMonths(1).AddDays(0);
var dback = "";
var dforw = "";
int dmovt = 0;
int cn;
@*Check querystring for date navigation +/- x months and pre links for next navigation*@
if (Request.QueryString["cn"] != null) {
int.TryParse(Request.QueryString["cn"], out cn);
StartDate1 = StartDate1.AddMonths(cn);
EndDate1 = EndDate1.AddMonths(cn);
dmovt = cn-1;
dback = "?cn="+dmovt.ToString();
dmovt = cn+1;
dforw = "?cn="+dmovt.ToString();
} else {
cn = 0;
dback = "?cn=-1";
dforw = "?cn=1";
}
@* Navigation and title at top of page *@
@:<div class="eventsHead">
@:<div class="CalNavRight"><a href="@dback" title="Previous month"><img src="/Theme/images/global/Arrow-Left-S.png" title="Previous month" /></a> @umbraco.library.FormatDateTime(StartDate, "MMMM") <a href="@dforw" title="Next month"><img src="/Theme/images/global/Arrow-Right-S.png" title="Next month" /></a></div>
@:<h1>@Model.pageTitle</h1>
@:</div>
@* initial prep for multi events on 1 day *@
@:<div><div><div>
@* pull all events from tree filtered by date params already set (for 1 month) *@
foreach (dynamic eventItem in node.Descendants().Where("eventStart > @0 && eventStart < @1 && nodeTypeAlias.Contains(\"Event\")", StartDate1, EndDate1).OrderBy("eventStart")) {
@* Check if event is ont he same day as the last, if so maintain the div *@
if (olddate.ToString() == eventItem.eventStart.ToString("ddMMyy")) {
@:<div class="events">
@:<a href="@eventItem.Url" class="" title="more about @eventItem.eventTtle">@eventItem.eventTtle @Xloc</a>
@:</div>
} else {
@* Creat new div for new date *@
@:</div></div></div>
@:<div class="EventCont">
@:<div class="eventDay" style="border: 2px solid @bgcolour;" >
@:<div class="eventInner">
@:<div class="eventTitle" style="background-color: @bgcolour;"><h2 class="tile event">@eventItem.eventStart.ToString("ddd d MMMM")</h2>
@:</div>
@:<div class="events">
@:<a href="@eventItem.Url" class="" title="more about @eventItem.eventTtle">@eventItem.eventTtle @Xloc</a>
@:</div>
}
olddate = eventItem.eventStart.ToString("ddMMyy");
}
@* Close final date container div *@
@:</div></div></div>
Hi Stephen
Happy that you found it useful. I used this reference by the way. http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/
=>Collections
Btw. you probably write macroScripts, so you can use a tidier syntax switching between razor and html. Like:
is working on a reply...