Fork me on GitHub

Code Snippet:

 1 open System
 2 
 3 type System.Int32 with
 4   member x.Months = DateTime.Today.AddMonths(x) - DateTime.Today
 5   member x.Years = DateTime.Today.AddYears(x) - DateTime.Today
 6 
 7 type System.Double with
 8   member x.Days = DateTime.Today.AddDays(x) - DateTime.Today
 9 
10 type System.TimeSpan with
11   member x.Ago = DateTime.Now.Add(-x)
12   member x.FromNow = DateTime.Now.Add(x)
13   member x.FromToday = DateTime.Today.Add(x)
14 
15 let foo = DateTime.Now - 20 .Years - 0.5 .Days
16 let bar = 50 .Years.Ago

Code output:

type Int32 with
  member Months : System.TimeSpan
type Int32 with
  member Years : System.TimeSpan
type Double with
  member Days : System.TimeSpan
type TimeSpan with
  member Ago : System.DateTime
type TimeSpan with
  member FromNow : System.DateTime
type TimeSpan with
  member FromToday : System.DateTime
> val foo : System.DateTime = 03/06/1994 10:26:59
> val bar : System.DateTime = 03/05/1964 22:28:15

References:

comments powered by Disqus