locked
Anyone using Nuget package Humanizer for converting datetime to friendlier messaging? RRS feed

  • Question

  • User-453926175 posted

    I'm trying to use Humanizer to make comments have friendlier messages like, "Posted 2 hours ago" instead of 11/11/2015 8:53AM.

    I'm able to get it to work when I do the following:

    var SomeTime = DateTime.Now;

    and then in the body

    @SomeTime.Humanize()

    However, I need to apply it to a query that calls back other data in the record.

    When I try it on @item.Submitted.Humanize()

    I get the error that Humanize isn't part of the definition for System.DateTime

    Any assistance is greatly appreciated.

    JBishop

    Wednesday, November 11, 2015 4:39 PM

Answers

  • User-821857111 posted

    Right - item is a dynamic object. You need to do a cast:

    @((DateTime)item.Submitted).Humanize()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 3:08 PM
  • User-453926175 posted

    Awesome Mike!

    I had to add another set of parenthesis to encapsulate the whole thing.

    @(((DateTime)item.Submitted).Humanize())

    Thanks for your assistance as always!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 3:49 PM

All replies

  • User325035487 posted

    did you try to assign item.Submitted to a variable and then tried to humanize the variable?

    DateTime? submitted;
    
    submitted = item.submitted != null ? item.submitted : null
    
    if(submitted!=null){
    submitted.Humanize().... 

    }

    Thursday, November 12, 2015 12:36 AM
  • User-821857111 posted

    When I try it on @item.Submitted.Humanize()

    I get the error that Humanize isn't part of the definition for System.DateTime

    Sounds like you need a using directive for Humanize in the file that the query calls

    Thursday, November 12, 2015 2:21 AM
  • User-453926175 posted

    That's the @using Humanizer; at the top of the page? Got that...

    Thursday, November 12, 2015 10:34 AM
  • User-821857111 posted

    Right - item is a dynamic object. You need to do a cast:

    @((DateTime)item.Submitted).Humanize()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 3:08 PM
  • User-453926175 posted

    Awesome Mike!

    I had to add another set of parenthesis to encapsulate the whole thing.

    @(((DateTime)item.Submitted).Humanize())

    Thanks for your assistance as always!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 3:49 PM
  • User325035487 posted

    You will have a null exception at some point when the item.Submitted is null . Check for null before casting.

    Friday, November 13, 2015 1:15 AM