How to get current month in Query

114 Views Asked by At

In Umbraco CMS I have created a 6 events. In that 3 were created in January and the other 3 were created in February.

I want to fetch the events created in current month. For that I have used query builder in Umbraco and the query was:

@{
    var selection = Umbraco.Content(Guid.Parse("9359beaa-b580-4f2c-8c8f-239f29e560b4"))
    .ChildrenOfType("aseanEventsItem")
    .Where(x => (x.CreateDate >= DateTime.Now.Date))
    .Where(x => x.IsVisible());
}
<ul>
    @foreach (var item in selection)
    {
        <li>
            <a href="@item.Url">@item.Name</a>
        </li>
    }
</ul>

It's working fine now. How I can get the current month?

1

There are 1 best solutions below

0
Nurhak Kaya On

You can use the Model.Content.Children to do that. Here is an example and the screenshot for you:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Core.Models
@using Umbraco.Web

@{
    Layout = null;

    var lastMonth = DateTime.Now.AddMonths(-1);

    var testRoot = Model.Content.Children
        .Where(x => x.CreateDate >= lastMonth);

    //var selection = Umbraco.Content(Guid.Parse("9359beaa-b580-4f2c-8c8f-239f29e560b4"))
    //    .ChildrenOfType("aseanEventsItem")
    //    .Where(x => (x.CreateDate >= DateTime.Now.Date))
    //    .Where(x => x.IsVisible());
}

<p>TEST template</p>

@foreach (var item in testRoot)
{
    <p>@item.Name - @item.CreateDate</p>
}

enter image description here