Copied to clipboard

Flag this post as spam?

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


  • Keith Petersen 67 posts 111 karma points
    Jan 28, 2012 @ 22:22
    Keith Petersen
    0

    Get difference between CreateDate and UpdateDate in Razor

    What's the best way to do this? Using umbraco.library.DateDiff doesn't seem to work. I'm not sure DateDiff works at all with CreateDate and UpdateDate, since it seems to expect a different format. http://our.umbraco.org/forum/developers/xslt/15697-Hide-nodes-which-haven%27t-been-updated

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 28, 2012 @ 23:24
    Chriztian Steinmeier
    0

    Hi Keith,

    The sample he posted wasn't sending dates to DateDiff() - it actually works fine with those properties (from XSLT, that is - but I can't see why it shouldn't work in Razor too).

    /Chriztian

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 07:21
    Rodion Novoselov
    1

    Hi, Keith, Both CreateDate and UpdateDate are regular non-dynamic properties of DynamicNode. Since that they are typed as DateTime and you cannot pass them to the DateDiff method because it expects strings. If you much want to use the very DateDiff you can do it getting {Create/Update}Date-s converted to strings:

    var diff = umbraco.library.DateDiff(Model.CreateDate.ToString("s"), Model.UpdateDate.ToString("s"), "m");

    However you absolutely don't need it - since these props are DateTime-typed and you can write simply:

    var diff = Model.UpdateDate - Model.CreateDate;

     

  • Keith Petersen 67 posts 111 karma points
    Jan 30, 2012 @ 18:33
    Keith Petersen
    0

    Thanks, this works. I'd assumed they were strings, but I guess I should've tried subtracting them as dates first. This leads me to a more fundamental question: http://our.umbraco.org/forum/developers/razor/28172-Best-way-to-check-type-of-properties-on-Razor-Model-object

Please Sign in or register to post replies

Write your reply to:

Draft