Copied to clipboard

Flag this post as spam?

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


  • David 27 posts 127 karma points
    Nov 18, 2019 @ 12:02
    David
    0

    How to check if a DateTime datatype is present or not?

    Hi all, I'm trying to write a condition where if a piece of content doesn't have a startDate, it will not display the date and only the title. However I can't seem how to figure out how to identify if a DateTime datatype is present or not. All the test examples I've used do not have startDate and are all showing as "01 January 0001"

    Any ideas?

      @foreach (var item in trainingCourses)
            {
                if (item.IsNull(item.startDate))
                    {
                    <li>
                        <a href="@item.Url"><h3>@item.Name</h3></a>
                    </li>    
                    }                          
                else
                    {
                    <li>
                        <a href="@item.Url"><h3>@item.Name</h3>
                        <p>@(item.GetPropertyValue<DateTime>("startDate").ToString("dd MMMM yyyy"))</p></a>
                    </li>
                    }
    
            }
    

    Thanks,

    David

  • Paul Griffiths 370 posts 1021 karma points
    Nov 20, 2019 @ 13:24
    Paul Griffiths
    100

    Hi David,

    Does the following work:

    @foreach (var item in trainingCourses)
    {
        if (item.GetPropertyValue<DateTime>("startDate") == null || item.GetPropertyValue<DateTime>("startDate") == DateTime.MinValue)
        {
            <li>
                <a href="@item.Url"><h3>@item.Name</h3></a>
            </li>
        }
        else
        {
            <li>
                <a href="@item.Url">
                    <h3>@item.Name</h3>
                    <p>@(item.GetPropertyValue<DateTime>("startDate").ToString("dd MMMM yyyy"))</p>
                </a>
            </li>
        }
    
    }
    

    Thanks

    Paul

  • David 27 posts 127 karma points
    Nov 21, 2019 @ 09:40
    David
    0

    Hi Paul,

    Thanks for your reply. This looks to have done the trick :)

    Thank you!

    David

  • Paul Griffiths 370 posts 1021 karma points
    Nov 21, 2019 @ 09:59
    Paul Griffiths
    1

    Hi David,

    Great to hear that it helped :).

    Thanks Paul

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies