Created
March 7, 2014 00:47
-
-
Save okaram/9402900 to your computer and use it in GitHub Desktop.
Playing with extension methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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