Copied to clipboard

Flag this post as spam?

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


  • Valter Ekholm 1 post 71 karma points
    Jun 08, 2016 @ 14:29
    Valter Ekholm
    0

    Getting a date-variable from a post and reformatting to yymmdd

    Hello. I'm new to Umbraco, and have learned how to make document-types, templates and contents to work togheter, thank to our teacher Robert. I now want to change the format by which a date is printed. I get format mm/dd/yyyy - but I want YYYY-dd-mm like 2016-06-08. I don't know how to get the post's (it's a blog post) UpdateDate and how to reformat it. Can you help? I have this code now for a post (template):

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.ArticleItem>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Master.cshtml";
    }
    <p>@Umbraco.Field("articleText")</p>
    @Umbraco.Field("updateDate", insertBefore: "Datum ändrad: ")
    
  • skiltz 501 posts 701 karma points
    Jun 08, 2016 @ 23:50
    skiltz
    0

    @Convert.ToDateTime(@Umbraco.Field("updateDate").ToString()).ToString("YYYY-dd-mm")

  • David Armitage 508 posts 2078 karma points
    Jun 09, 2016 @ 04:56
    David Armitage
    0

    Hi,

    Similar to above how I handle this is I usually create a class for my Umbraco documents.

    My class would have a date property.

    E.g

    public DateTime ExpiryDate { get; set; }
    

    I would then have a method that pulls through all my umbraco data and assigns it to my class properties.

    E.g

    ExpiryDate = DateTime.Parse(node.GetProperty("expiryDate").Value)
    

    You might want to do some null checks etc. I personally usually use a nullable date in my class DateTime?

    When it comes to displaying my data to the frontend I usually do this using razor script. You can simply use the date format like above.

    E.g.

    @Model.ExpiryDate.ToString("YYYY-dd-mm")
    

    Hope this helps

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft