Lua 两个日期之间的月份分割

Lua 两个日期之间的月份分割,lua,Lua,我需要一个循环,其中x和y是两个输入变量, 现在我想得到一个结果数组,其中这两个日期之间的所有时间段都以月为单位。 例如,x=1.1.2019和y=31.3.2019, 结果将类似于数组 2019年1月1日、2019年1月31日、2019年1月2日、2019年2月28日、2019年3月1日、2019年3月31日使用LUA编程语言 x = os.time{year=2019, month=1, day=1} y = os.time{year=2019, month=3, day=31} df =

我需要一个循环,其中x和y是两个输入变量, 现在我想得到一个结果数组,其中这两个日期之间的所有时间段都以月为单位。 例如,x=1.1.2019和y=31.3.2019, 结果将类似于数组 2019年1月1日、2019年1月31日、2019年1月2日、2019年2月28日、2019年3月1日、2019年3月31日使用LUA编程语言

x = os.time{year=2019, month=1, day=1}
y = os.time{year=2019, month=3, day=31}

df = {}
i = 2
sec_day = 24*60*60
df[1] = x
while x <= y do
     x = x +sec_day
     df[i] = x
     i = i+1
end

--loop to get unique months
hash={}
d_months = {}
for _,v in ipairs(df) do
    if (not hash[os.date('%m',v)]) then
        d_months[#d_months+1] = os.date('%m', v)
        hash[os.date('%m',v)] = true
    end
end

--now everything is set up and we can iterate over the months to get the start and end days of the month
min_d = {}
max_d = {}
for idx,month in ipairs(d_months) do
    min = nil
    max = nil
    for _,v in ipairs(df) do
         if os.date('%m', v) == month then
             min = min or v
             max = max or v
             min = min < v and min or v
             max = max > v and max or v
         end
     end
 min_d[idx] = os.date('%d.%m.%Y', min)
 max_d[idx] = os.date('%d.%m.%Y', max)
 print(os.date('%d.%m.%Y', min) .. " " .. os.date('%d.%m.%Y', max))
 end
如果你想让它持续几年,只需多做两个循环。一旦你需要找到独特的年份,那么你需要在最后一部分不仅比较几个月,而且比较几年


编辑:更改了输出格式。

由于lua的标准格式,我得到了此输出01/01/19 01/31/19 02/19 02/28/19 03/01/19 03/31/19 04/01/19 04/01/19。时间戳是正确的。如果要获得所需的输出,只需再编写一个循环,将所有内容按正确的顺序排列,而不是将格式设置为%x使用%d.%m.%Y获得所需的格式
01.01.2019 31.01.2019
01.02.2019 28.02.2019
01.03.2019 31.03.2019