我有一个问题,我正在挣扎几天。
我的开始日期为{2017, 9, 14}
,结束日期为{2018, 5, 1}
。
我需要的是从2017-9-14 to 2017-5-1
开始每天:
{2017, 9, 14}
{2017, 9, 15}
{2017, 9, 16}
[...]
{2017, 9, 30}
{2017, 10, 1}
[...]
{2018, 5, 1}
在Erlang中执行此操作的好方法是什么?
答案 0 :(得分:5)
Erlang中的calendar
模块具有将日期转换为从第0年开始的格里高利天数的功能:calendar:date_to_gregorian_days/1
和calendar:gregorian_days_to_date/1
以执行相反的操作。使用这些,加上lists:seq
,我们可以使用列表理解生成列表:
1> From = {2017, 9, 14}.
{2017,9,14}
2> To = {2018, 5, 1}.
{2018,5,1}
3> calendar:date_to_gregorian_days(From).
736951
4> calendar:date_to_gregorian_days(To).
737180
5> lists:seq(calendar:date_to_gregorian_days(From), calendar:date_to_gregorian_days(To)).
[736951,736952,736953,736954,736955,736956,736957,736958,
736959,736960,736961,736962,736963,736964,736965,736966,
736967,736968,736969,736970,736971,736972,736973,736974,
736975,736976,736977,736978,736979|...]
6> [calendar:gregorian_days_to_date(X) || X <- lists:seq(calendar:date_to_gregorian_days(From), calendar:date_to_gregorian_days(To))].
[{2017,9,14},
{2017,9,15},
{2017,9,16},
{2017,9,17},
{2017,9,18},
{2017,9,19},
{2017,9,20},
{2017,9,21},
{2017,9,22},
{2017,9,23},
{2017,9,24},
{2017,9,25},
{2017,9,26},
{2017,9,27},
{2017,9,28},
{2017,9,29},
{2017,9,30},
{2017,10,1},
{2017,10,2},
{2017,10,3},
{2017,10,4},
{2017,10,5},
{2017,10,6},
{2017,10,7},
{2017,10,8},
{2017,10,9},
{2017,10,...},
{2017,...},
{...}|...]