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: 
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 generate (start:DateTime) x = 
//    [1..x] |> List.map(fun i -> start.AddDays(float i))

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

More information

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