Skip to content

Instantly share code, notes, and snippets.

@okaram
Created March 7, 2014 00:47
Show Gist options
  • Save okaram/9402900 to your computer and use it in GitHub Desktop.
Save okaram/9402900 to your computer and use it in GitHub Desktop.
Playing with extension methods
public static class MyExtensions
{
public static System.TimeSpan Months(this Int32 x)
{
return System.DateTime.Today.AddMonths(x) - System.DateTime.Today;
}
public static System.TimeSpan Years(this Int32 x)
{
return System.DateTime.Today.AddYears(x) - System.DateTime.Today;
}
public static System.TimeSpan Years(this Double x)
{
return System.DateTime.Today.AddDays(x) - System.DateTime.Today;
}
public static System.DateTime Ago(this System.TimeSpan x)
{
return System.DateTime.Now.Add(x.Negate());
}
public static System.DateTime FromNow(this System.TimeSpan x)
{
return System.DateTime.Now.Add(x);
}
public static System.DateTime FromToday(this System.TimeSpan x)
{
return System.DateTime.Today.Add(x);
}
}
void Main()
{
Console.WriteLine(3.Months().Ago());
Console.WriteLine(3.Years().FromToday());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment