3 people like it.

Generate a date range sequence

Generates a sequence of dates (ascending or descending), incrementing (or decrementing) by one day at a time, inclusive of the start and end dates.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let dateRange (startDate : System.DateTime) (endDate : System.DateTime) =
    let start = startDate.Date
    let finish = endDate.Date
    if start = finish then Seq.singleton start
    else
        let shouldContinue =
            if start < finish then fun d -> d <= finish
            else fun d -> d >= finish
        let moveNext =
            if start < finish then fun (d : System.DateTime) -> d.AddDays(1.0)
            else fun (d : System.DateTime) -> d.AddDays(-1.0)
        let rec getNext (nextDate : System.DateTime) =
            seq {
                if shouldContinue nextDate then
                    yield nextDate
                    yield! getNext (nextDate |> moveNext)
            }
        getNext start
val dateRange : startDate:System.DateTime -> endDate:System.DateTime -> seq<System.DateTime>

Full name: Script.dateRange
val startDate : 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)
val endDate : System.DateTime
val start : System.DateTime
property System.DateTime.Date: System.DateTime
val finish : System.DateTime
module Seq

from Microsoft.FSharp.Collections
val singleton : value:'T -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.singleton
val shouldContinue : (System.DateTime -> bool)
val d : System.DateTime
val moveNext : (System.DateTime -> System.DateTime)
System.DateTime.AddDays(value: float) : System.DateTime
val getNext : (System.DateTime -> seq<System.DateTime>)
val nextDate : System.DateTime
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Raw view Test code New version

More information

Link:http://fssnip.net/oS
Posted:10 years ago
Author:Daniel Bradley
Tags: seqences , seq , datetime , dates