public static void Main(string[] args)
DateTime startDate = new DateTime(2016, 1, 1, 12, 1, 0);
DateTime endDate = new DateTime(2016, 1, 2, 12, 1, 0);
Console.Write(GetRelativeTimeBetweenDates(startDate, endDate));
public static string GetRelativeTimeBetweenDates(DateTime startDate, DateTime endDate)
const int MAXIMUM_DAYS_IN_MONTH = 31;
const int ONE_MINUTE = 60;
const int TWO_MINUTES = 120;
const int ONE_HOUR = 3600;
const int TWO_HOURS = 7200;
const int ONE_DAY = 86400;
const string DATE_FORMAT = "dd mmm yyyy";
TimeSpan interval = endDate.Subtract(startDate);
int differenceInDays = (int)interval.TotalDays;
int differenceInSeconds = (int)interval.TotalSeconds;
string relativeTime = "";
if (differenceInDays >= 0 && differenceInDays < MAXIMUM_DAYS_IN_MONTH)
if (differenceInDays == 0)
if (differenceInSeconds < ONE_MINUTE)
relativeTime = "just now";
else if (differenceInSeconds < TWO_MINUTES)
relativeTime = "1 minute ago";
else if (differenceInSeconds < ONE_HOUR)
relativeTime = string.Format("{0} minutes ago", Math.Floor((double)differenceInSeconds / ONE_MINUTE));
else if (differenceInSeconds < TWO_HOURS)
relativeTime = "1 hour ago";
else if (differenceInSeconds < ONE_DAY)
relativeTime = string.Format("{0} hours ago", Math.Floor((double)differenceInSeconds / ONE_HOUR));
else if (differenceInDays == 1)
DateTime yesterday = endDate.AddDays(-1);
if (startDate.Date == yesterday.Date)
relativeTime = "yesterday";
relativeTime = "2 days ago";
else if (differenceInDays < ONE_WEEK)
relativeTime = string.Format("{0} days ago", differenceInDays);
else if (differenceInDays < MAXIMUM_DAYS_IN_MONTH)
double numberOfWeeks = Math.Ceiling((double)differenceInDays / ONE_WEEK);
relativeTime = string.Format("{0} week{1} ago", numberOfWeeks, numberOfWeeks > 1 ? "s" : "");
DateTime oneMonthAgo = endDate.AddMonths(-1);
if (startDate.Year == oneMonthAgo.Year && startDate.Month == oneMonthAgo.Month)
relativeTime = "last month";
relativeTime = startDate.ToString(DATE_FORMAT);