25 people like it.

Human Date

Datetime Readable for Humans, Ported from Clojure https://github.com/pmn/noir-messageboard/blob/master/src/noir_messageboard/utils/utils.clj

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

let describeTimeElapsed span =
  match span with
  | (min, _, _, _, _) when min = 0.0 -> "just now"
  | (min, _, _, _, _) when min < 2.0 -> "a minute ago"
  | (min, _, _, _, _) when min < 60.0 -> sprintf "%g minutes ago" min
  | (_, hour, _, _, _) when hour = 1.0 -> "an hour ago"
  | (_, hour, _, _, _) when hour < 24.0 -> sprintf "%g hours ago" hour
  | (_, _, day, _, _) when day = 1.0 -> "yesterday"
  | (_, _, day, _, _) when day < 7.0 -> sprintf "%g days ago" (Math.Round(day))
  | (_, _, day, _, _) when Math.Round(float(day)/7.0) = 1.0 -> "1 week ago"
  | (_, _, day, _, _) when day < 31.0 -> sprintf "%g weeks ago" (Math.Round(day/7.0)) 
  | (_, _, _, months, _) when months = 1.0 -> "a month ago" 
  | (_, _, _, months, _) when months < 12.0 -> sprintf "%g months ago" months
  | (_, _, _, _, years) when years = 1.0 -> "a year ago"
  | (_, _, _, _, years) -> sprintf "%g years ago" years

let humandate (dt:DateTime) =
  let span = DateTime.Now.Subtract(dt)
  describeTimeElapsed (span.TotalMinutes, span.TotalHours, span.TotalDays, Math.Round(span.TotalMinutes/43829.0639), Math.Round(span.TotalMinutes/525948.766))
namespace System
val describeTimeElapsed : float * float * float * float * float -> string

Full name: Script.describeTimeElapsed
val span : float * float * float * float * float
val min : float
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val hour : float
val day : float
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
Math.Round(d: decimal) : decimal
Math.Round(a: float) : float
Math.Round(d: decimal, mode: MidpointRounding) : decimal
Math.Round(d: decimal, decimals: int) : decimal
Math.Round(value: float, mode: MidpointRounding) : float
Math.Round(value: float, digits: int) : float
Math.Round(d: decimal, decimals: int, mode: MidpointRounding) : decimal
Math.Round(value: float, digits: int, mode: MidpointRounding) : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val months : float
val years : float
val humandate : dt:DateTime -> string

Full name: Script.humandate
val dt : 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 span : TimeSpan
property DateTime.Now: DateTime
DateTime.Subtract(value: TimeSpan) : DateTime
DateTime.Subtract(value: DateTime) : TimeSpan
property TimeSpan.TotalMinutes: float
property TimeSpan.TotalHours: float
property TimeSpan.TotalDays: float

More information

Link:http://fssnip.net/99
Posted:12 years ago
Author:christophd
Tags: datetime , format , sprintf