允许一个日历年的验证 [英] validation for allowing one calendar year

查看:60
本文介绍了允许一个日历年的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查日期date1和date2:
日期间隔应为一个日历年

正常年份应检查365天,而leap年则应检查366天.

当我的date1是2012年3月1日,而date2是2013年2月28日时


I need to check my dates date1 and date2 :
dates interval should be one calendar year

meaning for normal year it should check for 365 days but for leap year it shud check for 366 days.

when my date1 is 1st march 2012 and date2 is 28 th feb 2013


If DateDiff(DateInterval.Day, date1, date2) > 365 Then
              
              Else
                
              End If

推荐答案

为什么?
我可以通过比较date1.AddYears(1)和date2来做到这一点-如果它小于或等于那么它就在容许范围之内.
这样,DateTime可以解决leap年问题,而您不必为此担心.
Why?
I would do it by comparing date1.AddYears(1) with date2 - if it is less than or equal then it''s within tolerance.
This way the DateTime takes care of leap years, and you don''t have to worry about it.


这篇关于允许一个日历年的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆