验证日期时间按月连续
我有一个包含 60 个 DateTime 的列表对象(按升序排序),并且需要验证每个日期是否比列表中的前一个日期大 1 个月。
例如,以下日期列表是有效的,因为它们增加一个月且没有缺失:
2009 年 1 月
2009 年 2 月
2009 年 3 月
2009 年 4 月
但是,以下日期列表无效,因为缺少 2009 年 2 月:
2009 年 1 月
2009 年 3 月
2009 年 4 月
日期并不重要,只考虑月和年。
有没有一种有效/漂亮的方法来做到这一点?
I have a list containing 60 DateTime objects (sorted in ascending order) and need to validate that each date is 1 month greater than the previous one in the list.
For example, the following list of dates would be valid because they increment by one month with none missing:
Jan-2009
Feb-2009
Mar-2009
Apr-2009
However, the following list of dates would be invalid because Feb-2009 is missing:
Jan-2009
Mar-2009
Apr-2009
The day doesn't matter, just the month and year are considered.
Is there an efficient/pretty way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于所有日期,如果采用(年 * 12 + 月),您将得到一个连续的整数列表。 这可能更容易检查间隙。
For all of the dates, if you take (year * 12 + month) you'll get a sequential list of integers. That might be easier to check for gaps.
您可以尝试以下操作:
这会将日期列表“转换”为代表年和月的数字,列表中的每个项目都应增加 1。 然后,我们从每个项目中减去当前索引,因此对于有效列表,所有项目都将具有相同的值。 然后,我们将所有值与
start
进行比较,这是第一个计算值。You could try the following:
This 'converts' the list of dates into a number that represents the Year and Month, which should be incrementing by 1 for each item in the list. We then subtract the current index from each of those items, so for a valid list, all items would have the same value. We then compare all values to
start
, which is the first computed value.这是一个干净的检查,使用精心设计的选择器,该选择器将根据您的用例进行正确比较:
使用此自定义
SequenceEqual
:Here's a clean check, making use of a carefully crafted selector that will compare correctly for your use case:
Using this custom
SequenceEqual
: