Search code examples
datehaskelldate-range

Create list of dates from start/end date in Haskell


Starting from the following date range:

startDate = "2020-01-01"
endDate = "2021-01-01"

What would be the most appropriate way to create a list of dates, say, weekly, from start to end?

result = ["2020-01-01", "2020-01-08", "2020-01-15", "2020-01-22"] -- and so on until 2021-01-01

Solution

  • import Data.Time.Calendar
    
    main :: IO ()
    main = print [fromGregorian 2020 1 1, fromGregorian 2020 1 8 .. fromGregorian 2021 1 1]
    
    [2020-01-01,2020-01-08,2020-01-15,2020-01-22,2020-01-29,2020-02-05,2020-02-12,2020-02-19,2020-02-26,2020-03-04,2020-03-11,2020-03-18,2020-03-25,2020-04-01,2020-04-08,2020-04-15,2020-04-22,2020-04-29,2020-05-06,2020-05-13,2020-05-20,2020-05-27,2020-06-03,2020-06-10,2020-06-17,2020-06-24,2020-07-01,2020-07-08,2020-07-15,2020-07-22,2020-07-29,2020-08-05,2020-08-12,2020-08-19,2020-08-26,2020-09-02,2020-09-09,2020-09-16,2020-09-23,2020-09-30,2020-10-07,2020-10-14,2020-10-21,2020-10-28,2020-11-04,2020-11-11,2020-11-18,2020-11-25,2020-12-02,2020-12-09,2020-12-16,2020-12-23,2020-12-30]
    

    [I'm not really a fan of this Show instance BTW, as that's totally not sensible Haskell code...]