Have you ever had a news page or a blog and instead of just showing the published date, you display how long ago it was published?
This blog was published 2 hours ago
Well, I had that requirement and this is how I accomplished it.
I created an Extension Method.
What is an Extension Method?
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.
source: Microsoft Documentation
I don't know about you but I sometimes find the Microsoft Documentation a bit over complicated, what I mean by that is, it's not the easiest to understand. Imagine if you are just learning C# and you read the above explanation, I'm not sure I would understand it.
What I take from this explanation is, an Extension Method allow you to extend the functionality of something that already exists.
So, that is what I did, I extended the functionality of the DateTime type and by making it an Extension method, I can use this anywhere in my project. Handy!
Setting the Extension method up.
From a setup point of view. In my project I created a new folder called Extensions. This is just for housekeeping, keep all the Extension methods together in one place.
I create a new class file called DateTimeExtensions.cs
The file itself looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyProject.Extensions
{
public static class DateTimeExtensions
{
public static string GetTimeSince(this DateTime objDateTime)
{
TimeSpan ts = DateTime.Now.ToUniversalTime().Subtract(objDateTime);
DateTime dt = DateTime.MinValue + ts;
int intDays = ts.Days;
int intHours = ts.Hours;
int intMinutes = ts.Minutes;
int intSeconds = ts.Seconds;
if (intDays > 30)
return string.Format("{0} months ago", dt.Month);
if (intDays == 1)
return string.Format("{0} day ago", intDays);
if (intDays > 0)
return string.Format("{0} days ago", intDays);
if (intHours > 0)
return string.Format("{0} hours ago", intHours);
if (intMinutes > 0)
return string.Format("{0} minutes ago", intMinutes);
if (intSeconds > 0)
return string.Format("{0} seconds ago", intSeconds);
// let's handle future times..just in case
if (intDays < 0)
return string.Format("in {0} days", Math.Abs(intDays));
if (intHours < 0)
return string.Format("in {0} hours", Math.Abs(intHours));
if (intMinutes < 0)
return string.Format("in {0} minutes", Math.Abs(intMinutes));
if (intSeconds < 0)
return string.Format("in {0} seconds", Math.Abs(intSeconds));
return "a bit";
}
}
}
This code was originally posted on thatsoftwaredude.com and I made a small change to it as it was showing
This blog was posted 1 days ago
which reads a bit oddly. I much prefer it to say
This blog was posted 1 day ago
I know, it's a small thing but it's something people tend to complain about.
Once you've saved the Extension Method you can then use it in your views, all you need to do is add @using MyProject.Extensions
to the top of your view and then you can pass a date in and I get the string returned :
DateTime postDate = article.GetPropertyValue<DateTime>("postDate");
string timeSince = postDate.GetTimeSince();
In the example above, I get the value of a property called postDate
from the backoffice and then use my Extension Method I created.
That's it.
I hope you find this useful.
Photo by Arnold Francisca on Unsplash
Published on: 14 January 2021