-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
  • 23,159
  • 5
  • 39
  • 56

1 Answers1

8

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,473
  • 22
  • 34