【1】实现代码
1 -- 第一种方式:简写 2 local dayAmount = os.date("%d", os.time({year = os.date("%Y"), month = os.date("%m") + 1, day = 0})) 3 print('The first way result : dayAmount = ' .. dayAmount) 4 5 -- 第二种方式:分写 6 local year, month = os.date("%Y", os.time()), os.date("%m", os.time()) + 1 -- 当前月份的下个月 7 local dayAmount = os.date("%d", os.time({year = year, month = month, day = 0})) -- 减去一天即day=0,再格式化 8 print ('The Second way result : year = '.. year .. ' month = ' .. month .. ' dayAmount = ' .. dayAmount)
注释:通过os.time()获取当前月份的下一个月减去1天(即当月最后一天)的时间,然后通过os.date()格式化时间,得到值即当月的总天数。
备注:亲测,可行.... 运行结果如下图:
Good Good Study, Day Day Up.
顺序 选择 循环 总结