如何验证输入日期是否在日期间隔中?

时间:2019-03-18 13:35:30

标签: c date dateinterval

我正在使用c语言,我们现在的课程是关于结构的。这是我的代码的一部分,“ temp”是一个结构,内部还具有日期结构。但是,在某些情况下,某些有效日期要等到最后一个条件才能输入。

if( temp.Date.year >= start.year &&  temp.Date.year <= end.year)    
if( (temp.Date.year >= start.year &&  temp.Date.year <= end.year) && (temp.Date.month >= start.month &&  temp.Date.month <= end.month) )    
if( (temp.Date.year >= start.year &&  temp.Date.year <= end.year) && (temp.Date.month >= start.month &&  temp.Date.month <= end.month) && temp.Date.day >= start.day &&  temp.Date.day <= end.day)
                        isDateValid = 1;

1 个答案:

答案 0 :(得分:1)

使用KISS方法。使其小而简单。

您可以使用一系列奇怪的条件,也可以简单地将日期转换为更方便的日期。

unsigned long start = start.Date.Year * 10000ul + start.Date.month * 100 + start.Date.day;

对温度和结束进行相同的操作。

这将为我们提供一些易于比较的数值YYYYMMDD。

if (start <= temp && temp <= end)
    isValid = true;

虽然这似乎是关于结构的练习,但是您可以将结构的使用限制为提取值。