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:
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.
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...):
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()
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).
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.
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
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:
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.
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);
thx Andi
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.
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
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).
Good luck!
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.
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.
is working on a reply...