Humanizer icon indicating copy to clipboard operation
Humanizer copied to clipboard

Time difference between DateTime.ToString() and DateTime.Humanize()?

Open Atulin opened this issue 4 years ago • 4 comments

I'm using Humanizer to show a human-readable time of posting a forum thread, like so:

<time class="date" 
      datetime="@Model.CreationDate.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)"
      title="@Model.CreationDate.ToString("dd MMMM yyyy HH:mm", CultureInfo.InvariantCulture)">
    @Model.CreationDate.Humanize(culture: CultureInfo.InvariantCulture)
</time>

Both .ToString() and .Humanize() use CultureInfo.InvariantCulture.

My system clock right now is

image

and I'm in UTC+1 timezone, but the HTML output I see is

<time class="date" datetime="2021-01-06 02:09" title="06 January 2021 02:09">
    49 minutes from now
</time>

so while .ToString() displays the correct time, .Humanize() seems to be displaying it incorrectly.

Atulin avatar Jan 06 '21 01:01 Atulin

I'm also having this issue and had hoped to come here and find a solution.

<div class="contenthub-grid-item-content">
            <h3 class="contenthub-grid-item-title">@Model.BlogPostTitle</h3>
            <p class="contenthub-grid-item-time">@Model.PostDate.Humanize().Transform(To.SentenceCase)</p>
        </div>

Model.PostDate returns Date = {5/18/2021 10:33:14 AM} but the Humanized time is saying "40 minutes from now"

OwainWilliams avatar May 18 '21 09:05 OwainWilliams

Fixed it by passing a false value to Humanize -

 string timeSince = postDate.Humanize(false).Transform(To.SentenceCase);

This seems to work by not using UTC timezone.

OwainWilliams avatar May 24 '21 09:05 OwainWilliams

That did the trick, thanks!

Atulin avatar May 25 '21 18:05 Atulin

Fixed it by passing a false value to Humanize -


 string timeSince = postDate.Humanize(false).Transform(To.SentenceCase);

This seems to work by not using UTC timezone.

How to set manually comparison time ?

zanyar3 avatar Jul 21 '22 17:07 zanyar3