Copied to clipboard

Flag this post as spam?

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


  • Mikkel 14 posts 115 karma points
    Aug 02, 2018 @ 13:35
    Mikkel
    0

    Format of Umbraco.Date property

    Hey!

    So I've been trying to format the output of my Umbraco.Date property. The date is extracted from a selection of content pages, and is to be posted on my frontpage alongside with other content from those content pages.

    All of this is gathered in a PartialViewMacro.

    Alias of my Umbraco.Date property: releaseDate

    Format I need: dd-MM-yyyy

    What I can get it to output: M/dd/yyyy hh:mm:ss a

    I've tried using .ToString("dd-MM-yyyy"), but with no success.

    I've tried adding <DateTime> at the end of my .GetPropertyValue

    And I've tried with .getProperty("releaseDate").Value.ToString and others.

    None of them seem to work, in my case.

    Current code:

       @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ 
        var selection = Model.Content.AncestorOrSelf().Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "randomAlias").OrderBy("UpdateDate desc").Take(6);
    }
    <section id="randomID">
    @if (selection != null) {
        <h2>randomHeader</h2>
        <ul class="clearfix">
            @foreach (var item in selection)
            {
                var logo = item.GetPropertyValue<IPublishedContent>("image");
                if(logo != null)
                {
                    <li class="randomClasses">
                        <a href="@item.Url">
                            <p class="randomHeader2">@item.GetPropertyValue("h1")</p>
                            <img src="@logo.Url" alt='@item.GetPropertyValue("imgalt")' />
                            <div class="pressCat">@item.Parent.Name</div>
                            <div class="releaseDate">@item.GetPropertyValue("releaseDate")</div>
                        </a>
                    </li>
                }
            }
        </ul>
    }
    </section>
    

    Was hoping any of you could see what I've been doing wrong. Most likely something very silly I've missed, but I can't seem to find the problem.

    Hope anyone's able to solve this for me.

    Thanks in advance!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 02, 2018 @ 14:31
    Jan Skovgaard
    100

    Hi Mikkel

    One way of doing it is by using FormatDateTime method in the umbraco.library.

    Your release date can the be formatted like this

    <time>@umbraco.library.FormatDateTime(Model.Content.GetPropertyValue("releaseDate").ToString(), "dd-MM-yyyy")</time>
    

    So your above example would be

               @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
            @{ 
                var selection = Model.Content.AncestorOrSelf().Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "randomAlias").OrderBy("UpdateDate desc").Take(6);
            }
            <section id="randomID">
            @if (selection != null) {
                <h2>randomHeader</h2>
                <ul class="clearfix">
                    @foreach (var item in selection)
                    {
                        var logo = item.GetPropertyValue<IPublishedContent>("image");
                        if(logo != null)
                        {
                            <li class="randomClasses">
                                <a href="@item.Url">
                                    <p class="randomHeader2">@item.GetPropertyValue("h1")</p>
                                    <img src="@logo.Url" alt='@item.GetPropertyValue("imgalt")' />
                                    <div class="pressCat">@item.Parent.Name</div
    <time>@umbraco.library.FormatDateTime(item.GetPropertyValue("releaseDate").ToString(), "dd-MM-yyyy")</time>
                                </a>
                            </li>
                        }
                    }
                </ul>
            }
            </section>
    

    I hope this works for you :)

    /Jan

  • Mikkel 14 posts 115 karma points
    Aug 03, 2018 @ 06:55
    Mikkel
    0

    This works perfectly, thanks a lot! :D

Please Sign in or register to post replies

Write your reply to:

Draft