5 people like it.

Generate a date range sequence (another alternative)

Generates a sequence of dates (ascending or descending), incrementing (or decrementing) by one day at a time, inclusive of the start and end dates. This is an alternative to http://www.fssnip.net/oS

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
let dateRange startDate (endDate:System.DateTime) = 
  Seq.unfold (function 
     | acc when acc < endDate.AddDays(1.) -> Some(acc, acc.AddDays(1.))
     | acc when acc > endDate -> Some(acc, acc.AddDays(-1.))
     | acc -> None) startDate

// Another bit different approach:
// This version generates a fixed number of dates, does not have an endDate parameter
let dateRange2 (start:System.DateTime) x = 
    [1..x] |> List.map(fun i -> start.AddDays(float i))

// This version has an endDate parameter
let dateRange3 (startDate: System.DateTime) endDate =
     Seq.initInfinite float
     |> Seq.map (fun i -> startDate.AddDays i)
     |> Seq.takeWhile (fun dt -> dt <= endDate)

/// Creates an increasing sequence with all dates between two dates (inclusive).
let createDateRange4 (date1:System.DateTime) date2 =
    let start = min date1 date2
    let totalDays = (date2 - date1).TotalDays |> abs |> int |> (+) 1
    Seq.init totalDays (float >> start.AddDays)
val dateRange : startDate:System.DateTime -> endDate:System.DateTime -> seq<System.DateTime>

Full name: Script.dateRange
val startDate : System.DateTime
val endDate : System.DateTime
namespace System
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

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
module Seq

from Microsoft.FSharp.Collections
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
val acc : System.DateTime
System.DateTime.AddDays(value: float) : System.DateTime
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val dateRange2 : start:System.DateTime -> x:int -> System.DateTime list

Full name: Script.dateRange2
val start : System.DateTime
val x : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val i : int
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
val dateRange3 : startDate:System.DateTime -> endDate:System.DateTime -> seq<System.DateTime>

Full name: Script.dateRange3
val initInfinite : initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.initInfinite
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val i : float
val takeWhile : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.takeWhile
val dt : System.DateTime
val createDateRange4 : date1:System.DateTime -> date2:System.DateTime -> seq<System.DateTime>

Full name: Script.createDateRange4


 Creates an increasing sequence with all dates between two dates (inclusive).
val date1 : System.DateTime
val date2 : System.DateTime
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val totalDays : int
val abs : value:'T -> 'T (requires member Abs)

Full name: Microsoft.FSharp.Core.Operators.abs
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<_>
val init : count:int -> initializer:(int -> 'T) -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.init

More information

Link:http://fssnip.net/oW
Posted:7 years ago
Author:Fernando Saldanha
Tags: seq , datetime , dates , sequences