Copied to clipboard

Flag this post as spam?

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


  • Andreas Kaltenhuber 107 posts 286 karma points
    Jun 20, 2011 @ 15:10
    Andreas Kaltenhuber
    0

    Razor .Where DateTime

    Hi together,

    i've an event list with value "np_event_datetime" of DatePicker (user can only choose a Date, about the time we don't care).

    Now i would like to highlight all the days in a calendar where there is an/more event/s, but my problem is how to make this query

    i've something like this (to check if there are events today)

    Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where("np_event_datetime > DateTime.Now");

    But in this case the 2 values are DateTime Objects so this doesn't work. i just need to check if the "shortdatetime" value of both fields is the same!

    any ideas? thx in advance, andi

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 20, 2011 @ 21:42
    Dan Diplo
    0

    Hi,

    If I understand you correctly you want to get all the nodes that have dates that are equal to today's date (without the time component)? Then you should be able to do this:

     Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where("np_event_datetime == DateTime.Now.Date");

    The important part is to use DateTime.Now.Date which gets just the date part of the current date. Then you can just use a standard equivalence comparison.

  • Andreas Kaltenhuber 107 posts 286 karma points
    Jun 21, 2011 @ 09:44
    Andreas Kaltenhuber
    0

    Hi Dan,

    i tried this already, but with no success! This returns zero matches in my case

    At the moment i have a working solutions, not the way i wanted, but it does the job:

    var values new Dictionary<string,object>();
    values.Add("keywords",DateTime.Now.Date);
    Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where(
    "np_event_datetime.Contains(keywords)"values)

    thx Andi


  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jun 21, 2011 @ 10:26
    Michael Latouche
    0

    Hi Andreas,

    Is your np_event_datetime an actual DateTime, or a string representation of it? What might be the cause of the problem is that, even if you select only a date and no time, you end up with a string time part of "00:00:00", which might explain why the proposition of Dan does not work and your dictionary one well.

    So maybe one of the following would work (sorry for the layout, for some reason I do not have the styke "code" available in the style dropdown...):

    Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where("np_event_datetime.Date == DateTime.Now.Date");

    or

    Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where("np_event_datetime == DateTime.Now.Date.ToString()");

    or

     

    Model.AncestorOrSelf(1).Descendants("np_doctype_event")
    .Where("umbracoNaviHide == false")
    .Where("np_event_datetime.ToShortDateString() == DateTime.Now.Date.ToShortDateString()");

    Hope this helps.

    Cheers,

     

    Michael.

  • Andreas Kaltenhuber 107 posts 286 karma points
    Jun 21, 2011 @ 10:41
    Andreas Kaltenhuber
    0

    Hi Michael,

    i tried all your suggestions before but with no luck. To get all the events of a certain date (ignoring the time part) i thought of a string comparison on the shortdate value too.

    but this throws up errors. i cannt event use DateTime.Now.ToShortDateString()

    maybe there is some information here: http://umbraco.com/follow-us/blog-archive/2011/3/13/umbraco-razor-feature-walkthrough-part-5

    anyway. maybe anybody else has some ideas on this issue?


    cheers,

    Andi

     

    PS: I'm using the latest Version 4.7

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 21, 2011 @ 12:03
    Dan Diplo
    0

    Hi Andreas,

    I did test the code I posted using some example pages I created with a custom datefield that used the standard Umbraco Date Picker data-type in a new 4.7 site. It did work as expected in that it brought back all nodes in the query that matched the current date when I used DateTime.Now.Date as the comparison. I am confident this works.

    I would suggest making your query as simple as possible first and checking the nodes it returns to ensure that there date component (np_event_datetime) has some that match the current date. Then the "where" part should definitely work (if your np_event_datetime is a standard date-picker field).


    .Where("np_event_datetime == DateTime.Now.Date")
     

    Good luck!

     

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jun 21, 2011 @ 12:11
    Michael Latouche
    0

    Hi Andreas,

    If you have not done so yet, it might also be a good idea to output the value of the np_event_date, without any date filter, to see what it actually contains/returns. This might help you see why the comparison of Dan does not work in your case.

    Cheers,

    Michael.

  • Toni Becker 146 posts 425 karma points
    Jul 30, 2011 @ 15:09
    Toni Becker
    0

    Juhu everybody. Working on a client project i did the same.

    I#m using macroEngines from 4.7.1 but i think that's not the problem.

    I've done the following

    @foreach {dynamic node in myDefinedNodelist.Take(10).OrderBy("Name desc")

    {
    var date1 = DateTime.Now.ToString("ddMMMyyyy");
    var date2 = node.CreateDate.ToString("ddMMMyyyy");

    if(date1 == date2)
    {
    @do something
    }
    else
    {
    show nothing
    }
    ...
    }

     

    Thats workin for me. Hope this helps.

Please Sign in or register to post replies

Write your reply to:

Draft