Copied to clipboard

Flag this post as spam?

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


  • Thomas 315 posts 602 karma points c-trib
    Sep 14, 2017 @ 13:00
    Thomas
    0

    Date - time datatype output

    Hej

    Is there a way to change the time format from:

    9/13/2017 2:30:00 PM

    to

    9/13/2017 14:30 PM ??

  • Eric Frost 5 posts 88 karma points admin hq
    Sep 15, 2017 @ 07:59
    Eric Frost
    1

    Hi Thomas,

    Maybe this might work for you:

    string formatted = dt_calc.ToString("HH:mm:ss");

    and if you using some custom data you should definitely look up Microsoft documentation on custom Datetime. https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

  • Lewis Smith 208 posts 617 karma points c-trib
    Sep 15, 2017 @ 08:34
    Lewis Smith
    101

    Hi Thomas,

    You could do the following:

    When retrieving the date from your cms:

    var date = Model.GetPropertyValue<DateTime>("dateInputGoesHere");
    

    (this may be some different syntax depending on the type of razor you are using) but you mainly need to render the item as a DateTime to be aloud to use the .ToString below.

    <p>@date.ToString("d MMM yy")</p>
    

    You can read up on date formatting here: https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.80).aspx

    If you show me some code samples you are working with I can try to resolve them for you.

    Thanks, Lewis

  • Thomas 315 posts 602 karma points c-trib
    Oct 05, 2017 @ 11:21
    Thomas
    0

    Thanks,

    ended up doing it like you showed!

    var date = Model.Content.GetPropertyValue<DateTime>("pageDate");
    
        if (date != DateTime.MinValue)
        {
           <small>@date.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture)</small>
        }
    
  • Matthew Taylor 11 posts 85 karma points
    Jul 31, 2019 @ 13:54
    Matthew Taylor
    2

    In Umbraco 8 you can get the formatting from https://momentjs.com/

    Then use something like;

            @if (Model.HasValue("yourDateTimeProperty"))
            {
                @(Model.Value<DateTime>("yourDateTimeProperty").ToString("dd MM yyyy"))
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft