Copied to clipboard

Flag this post as spam?

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


  • Bent Holz 100 posts 273 karma points
    Feb 07, 2018 @ 10:35
    Bent Holz
    0

    Problem with showing date from date picker

    I'm trying to create a very simple list of articles. On my doctype I have a property called "articleDate" with a Date editor set to mandatory.

    My partial view looks like this:

    @{ var selection = Model.Content.Children.Where(x => x.IsVisible()).ToArray(); }    
    @if (selection.Length > 0)
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li>
                        <a href="@item.Url">@Umbraco.Field("articleDate", formatAsDate: true) - @item.Name</a>
                    </li>
                }
            </ul>
        }
    

    But for some reason when outputting the list it replaces "articleDate" with the current date?!?... Am I doing something wrong and if yes.... what?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 08, 2018 @ 10:18
    Dave Woestenborghs
    0

    Hi Bent,

    You can also do this

    @item.GetPropertyValue<DateTime>("articleDate").ToString("dd/MM/yyyy")
    

    Of course you need to change the format in the ToString to your desired format

    Dave

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Feb 08, 2018 @ 10:21
    Steve Morgan
    101

    Hi Bent,

    You're using @Umbraco.Field which will attempt to get the data from the current page node not the children so you want to use the item!

    You want something like:

    @item.GetPropertyValue<DateTime>("articleDate")
    

    And you can control the format (I think you're in the US?) via something

    like:

    @(item<DateTime>("articleDate").ToString("MMM yyyy ddd d HH:mm"))
    

    Once your razor to format starts looking this complicated it's probably worth breaking it out a bit. So something like:

        @{ var selection = Model.Content.Children.Where(x => x.IsVisible()).ToArray(); }    
    @if (selection.Length > 0)
        {
            <ul>
                @foreach (var item in selection)
                {
                    var formatedArticleDate = item.GetPropertyValue<DateTime>("articleDate").ToString("MMM yyyy ddd d HH:mm");
                    <li>
                        <a href="@item.Url">@formatedArticleDate - @item.Name</a>
                    </li>
                }
            </ul>
        }
    

    HTH

    Steve

    EDIT: Dave is much faster at typing than me

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Feb 08, 2018 @ 10:22
    Dave Woestenborghs
    2

    EDIT: Dave is much faster at typing than me

    Probably because my answer is way shorter and not explaining why his approach didn't work :-)

    Dave

  • Bent Holz 100 posts 273 karma points
    Feb 08, 2018 @ 10:49
    Bent Holz
    0

    Of course... And very nice answer :)

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft