1

I'm working on simple asp.net mvc website, that shows user's post. I'm saving the post details in the DB including uploaded date.

DateTime dateTimeNow = DateTime.Now;
newPost.Feed_Upload_Date = dateTimeNow;
DB.Post.Add(newPost);
DB.SaveChanges();

Now in my View when I'm showing the date, it shows me something like this

"1/21/2019 4:29:58 PM"

What I want to achieve is to convert this time in my Database into something like "1 day ago". Thanks.

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
Alaa Hershey
  • 85
  • 1
  • 9

1 Answers1

9

You can either implement it yourself or use Humanizer library:

DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"
DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"
DateTimeOffset.UtcNow.AddHours(1).Humanize() => "an hour from now"
Karel Frajták
  • 4,389
  • 1
  • 23
  • 34