42 people like it.

Friendly date formatting

Generates a friendly string describing a date relatively to the current date and time. The function returns strings like "2 secs ago", "yesterday" or "3 years ago".

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open System

/// Generates a friendly string describing a date relatively to the current
/// date and time. The function returns strings like "X mins ago" (or secs, 
/// hours, days, months, years) or "yesterday". It is not completely precise
/// (e.g. doesn't take leap years into account)
let formatFriendlyDate (dt:DateTime) = 
  let ts = DateTime.UtcNow - dt
  if ts.TotalSeconds < 60.0 then sprintf "%d secs ago" (int ts.TotalSeconds)
  elif ts.TotalMinutes < 60.0 then sprintf "%d mins ago" (int ts.TotalMinutes)
  elif ts.TotalHours < 24.0 then sprintf "%d hours ago" (int ts.TotalHours)
  elif ts.TotalHours < 48.0 then sprintf "yesterday"
  elif ts.TotalDays < 30.0 then sprintf "%d days ago" (int ts.TotalDays)
  elif ts.TotalDays < 365.0 then sprintf "%d months ago" (int ts.TotalDays / 30)
  else sprintf "%d years ago" (int ts.TotalDays / 365)
namespace System
val formatFriendlyDate : dt:DateTime -> string

Full name: Script.formatFriendlyDate


 Generates a friendly string describing a date relatively to the current
 date and time. The function returns strings like "X mins ago" (or secs,
 hours, days, months, years) or "yesterday". It is not completely precise
 (e.g. doesn't take leap years into account)
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 ts : TimeSpan
property DateTime.UtcNow: DateTime
property TimeSpan.TotalSeconds: float
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
property TimeSpan.TotalMinutes: float
property TimeSpan.TotalHours: float
property TimeSpan.TotalDays: float
Raw view Test code New version

More information

Link:http://fssnip.net/s
Posted:14 years ago
Author:Tomas Petricek
Tags: datetime , format , sprintf