Skip to content

Instantly share code, notes, and snippets.

@veggerby
Created July 5, 2011 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veggerby/1064442 to your computer and use it in GitHub Desktop.
Save veggerby/1064442 to your computer and use it in GitHub Desktop.
Get relative time string from a date
public static string ToRelativeTime(this DateTime from)
{
DateTime now = DateTime.Now;
return from.ToRelativeTime(now);
}
public static string ToRelativeTime(this DateTime from, bool usePreAndSuffix)
{
DateTime now = DateTime.Now;
return from.ToRelativeTime(now, usePreAndSuffix);
}
public static string ToRelativeTime(this DateTime from, DateTime to)
{
return from.ToRelativeTime(to, true);
}
public static string ToRelativeTime(this DateTime from, DateTime to, bool usePreAndSuffix)
{
string prefix = usePreAndSuffix && (from > to) ? "in " : string.Empty;
string suffix = !usePreAndSuffix || (from > to) ? string.Empty : " ago";
// is more than 1 year?
if (from > to)
{
DateTime d = from;
from = to;
to = d;
}
int years = to.Year - from.Year;
if ((to.Month < from.Month) || ((to.Month == from.Month) && (to.Day < from.Day)))
{
years--;
}
if (years > 1)
{
return string.Format("{0}{1} years{2}", prefix, years, suffix);
}
else if (years == 1)
{
return string.Format("{0}1 year{1}", prefix, suffix);
}
// less than 1 year, is more than 1 month?
int months = to.Month - from.Month;
if (months < 0)
{
months += 12;
}
if ((to.Day < from.Day) || ((to.Day == from.Day) && (to.TimeOfDay < from.TimeOfDay)))
{
months--;
}
if (months > 1)
{
return string.Format("{0}{1} months{2}", prefix, months, suffix);
}
else if (months == 1)
{
return string.Format("{0}1 month{1}", prefix, suffix);
}
// less than 1 month, is more than 1 day/week?
TimeSpan diff = to - from;
if (diff.Days > 7)
{
return string.Format("{0}{1} weeks{2}", prefix, diff.Days / 7, suffix);
}
else if (diff.Days > 1)
{
return string.Format("{0}{1} days{2}", prefix, diff.Days, suffix);
}
else if (diff.Days == 1)
{
return string.Format("{0}1 day{1}", prefix, suffix);
}
// less than 1 day, is more than 1 hour?
if (diff.Hours > 1)
{
return string.Format("{0}{1} hours{2}", prefix, diff.Hours, suffix);
}
else if (diff.Hours == 1)
{
return string.Format("{0}1 hour{1}", prefix, suffix);
}
// less than 1 hour, is more than 1 minute?
if (diff.Minutes > 1)
{
return string.Format("{0}{1} minutes{2}", prefix, diff.Minutes, suffix);
}
else if (diff.Minutes == 1)
{
return string.Format("{0}1 minute{1}", prefix, suffix);
}
// less than 1 minute
if (diff.Seconds == 1)
{
return string.Format("{0}1 second{1}", prefix, suffix);
}
else if (diff.Seconds < 1)
{
return string.Format("{0}less than 1 second{1}", prefix, suffix);
}
return string.Format("{0}{1} seconds{2}", prefix, diff.Seconds, suffix);
}
@veggerby
Copy link
Author

veggerby commented Jul 5, 2011

Not localized

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment