gpt4 book ai didi

Erlang 开始日期和结束日期之间每天的日期范围间隔

转载 作者:行者123 更新时间:2023-12-01 11:16:33 26 4
gpt4 key购买 nike

我有一个问题困扰了几天。

我有一个开始日期{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 中执行此操作的好方法是什么?

最佳答案

calendar Erlang 中的模块具有将日期转换为从 0 年开始的公历天数的功能:calendar:date_to_gregorian_days/1calendar: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,...},
{...}|...]

关于Erlang 开始日期和结束日期之间每天的日期范围间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50200180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com