Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Last active July 6, 2018 12:53
Show Gist options
  • Select an option

  • Save ramonsmits/5594942 to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/5594942 to your computer and use it in GitHub Desktop.
Some simple TimeSpan extension methods that add approximations for total weeks, months and years in a timespan.
using System;
public static class TimeSpanExtensions
{
const double DaysInWeek = 7d;
const double DaysInMonth = 365d / 12d;
const double DaysInYear = 365d;
public static double TotalWeeks(this TimeSpan val)
{
return val.TotalDays / DaysInWeek;
}
public static double TotalMonths(this TimeSpan val)
{
return val.TotalDays / DaysInMonth;
}
public static double TotalYears(this TimeSpan val)
{
return val.TotalDays / DaysInYear;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment