Copied to clipboard

Flag this post as spam?

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


  • Alec Rust 11 posts 31 karma points
    Feb 28, 2012 @ 17:10
    Alec Rust
    0

    Output correct "datetime" <time> attribute

    I'm looking to output a timestamp of when content was published within Umbraco, in the correct format required by the HTML5 <time> element.

    For example:

    <time datetime="2012-02-28T20:00+00:00" pubdate>February 28, 2012</time>

    Is there a way to achieve this with Razor? I know with PHP there's a particular "W3C" way of outputting timestamps (http://goo.gl/sEFsh).

    More info on correct formatting of this <time> element can be found here: http://html5doctor.com/the-time-element/

  • Rodion Novoselov 694 posts 859 karma points
    Feb 28, 2012 @ 18:51
    Rodion Novoselov
    0

    Hi. I think you could just use usual DateTime.ToString(formatString) with format string "yyyy-MM-ddThh:mm+zzzz". Like:

    <time datetime="@Model.UpdateDate.ToString("yyyy-MM-ddThh:mm+zzz")">

     

  • Alec Rust 11 posts 31 karma points
    Feb 29, 2012 @ 11:12
    Alec Rust
    0

    Thanks for your response!

    Unfortunately that doesn't work for me, although this does:

    @{
    var now = DateTime.Now;
    }

    <time datetime="@now.ToString("o")" pubdate>@now.ToLongDateString()</time>

    That gives me this output:

    <time datetime="2012-02-29T10:06:33.9228516+00:00" pubdate>29 February 2012</time>

    I'm now trying to figure out a way to remove the unnecessary digits from the time string and have it output the date published rather than the current date!

  • Alec Rust 11 posts 31 karma points
    Feb 29, 2012 @ 11:27
    Alec Rust
    0

    In addition, this code seems to work within a .cshtml Scripting File, but not within a page template. Does it need to be formatted slightly differently for this?

Please Sign in or register to post replies

Write your reply to:

Draft