Copied to clipboard

Flag this post as spam?

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


  • Stephen 94 posts 255 karma points
    Jul 22, 2014 @ 18:44
    Stephen
    0

    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")) {

    None of these do, and I can't see why:

    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

     

  • Stephen 94 posts 255 karma points
    Jul 22, 2014 @ 18:57
    Stephen
    0

    Forgot to mention.  Umbraco 7 using Razor (not MVC).

  • Allan Molsen Larsen 22 posts 192 karma points
    Jul 23, 2014 @ 09:48
    Allan Molsen Larsen
    100

    Hi Stephen

    Try parsing in your parameter like this:

    foreach (dynamic eventItem in node.Descendants().Where("eventStart > @0 && nodeTypeAlias.Contains(\"Event\")", noww ).OrderBy("eventStart")) {
    
  • Stephen 94 posts 255 karma points
    Jul 23, 2014 @ 16:26
    Stephen
    0

    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>

    ----------------------------------------------------------------------
    Not the tidiest code ever, but hopefully will help someone else with a calendar solution, as there is basically nothing for Umbraco 7 yet.
  • Allan Molsen Larsen 22 posts 192 karma points
    Jul 25, 2014 @ 09:58
    Allan Molsen Larsen
    0

    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:

    @{
     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 ...
    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft