27 people like it.

Calculating Age

This function interprets a time span in terms of years, months and days.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
open System

let getAge (d : DateTime) =
        let d' = DateTime.Now
        match d' > d with
        | true ->
            let months = 12 * (d'.Year - d.Year) + (d'.Month - d.Month)
 
            match d'.Day < d.Day with
            | true -> let days = DateTime.DaysInMonth(d.Year, d.Month) - d.Day + d'.Day
                      let years = (months - 1) / 12
                      let months' = (months - 1) - years * 12
                      (years, months', days)
            | false -> let days = d'.Day - d.Day
                       let years = months / 12
                       let months' = months - years * 12
                       (years, months', days)
 
        | false -> (0,0,0)

// Example
let birthDate = DateTime.Parse("2/8/1995")
let y, m, d = getAge birthDate
printfn "Age: %d years, %d months and %d days." y m d

// Output
// Age: 15 years, 5 months and 6 days. 
namespace System
val getAge : d:DateTime -> int * int * int

Full name: Script.getAge
val d : DateTime
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
val d' : DateTime
property DateTime.Now: DateTime
val months : int
property DateTime.Year: int
property DateTime.Month: int
property DateTime.Day: int
val days : int
DateTime.DaysInMonth(year: int, month: int) : int
val years : int
val months' : int
val birthDate : DateTime

Full name: Script.birthDate
DateTime.Parse(s: string) : DateTime
DateTime.Parse(s: string, provider: IFormatProvider) : DateTime
DateTime.Parse(s: string, provider: IFormatProvider, styles: Globalization.DateTimeStyles) : DateTime
val y : int

Full name: Script.y
val m : int

Full name: Script.m
val d : int

Full name: Script.d
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

More information

Link:http://fssnip.net/1G
Posted:6 years ago
Author:Taha Hachana
Tags: timespan