用C两个日期之间的差异 [英] Difference between two dates in C
问题描述
我在C初学者。
- 是否有日期的任何数据类型?
- 在C中有与时间的工作,是有日期它太?
- 如何计算差异两个日期之间?
是否有日期的任何数据类型?
块引用>没有,在
ç内置的数据类型
,你必须定义用户定义的数据类型。
我如何计算两个日期之间的区别?
块引用>您可以试试这个:
结构DT
{
INT DD;
诠释毫米;
INT YY;
};
DT的typedef日期;在main()中,你需要声明的类型三个变量
数据
。结果
在下面的例子中今天
的差异,结果
比如你要利用当前日期之间的差异(c_date
)和出生日期(DOB
)日期DOB,c_date,今天; 如果(c_date.dd> = dob.dd)
today.dd = c_date.dd,dob.dd;
其他
{
c_date.dd + = 30;
c_date.mm- = 1;
today.dd = c_date.dd,dob.dd;
}
如果(c_date.mm> = dob.mm)
today.mm = c_date.mm-dob.mm;
其他
{
c_date.mm + = 12;
c_date.yy- = 1;
today.mm = c_date.dd-dob.mm;
}
today.yy = c_date.yy,dob.yy;在
今天
你有两个日期之间的差异。还有一个办法:
双difftime(time_t的结束,time_t的开始);
结果
阅读本答案:结果
1。<一个href=\"http://stackoverflow.com/questions/15555406/how-to-compare-two-time-stamp-in-format-month-date-hhmmss-to-check-ve-or-v/15556395#15556395\">How比较格式两个时间戳记月日HH:MM:SS结果
2. 如何找到以小时为单位的两个日期之间的差别,在C?I'm a beginner in C.
- Is there any datatype for dates?
- In C we have for working with time, is there one for dates too?
- How can I calculate difference between two dates?
解决方案Is there any datatype for dates?
No, inbuilt datatype in
C
, you have to defined user-defined data type.How can I calculate difference between two dates?
You may try this:
struct dt { int dd; int mm; int yy; }; typedef dt date;
In main() you need to declare three variables for type
data
.
In following exampletoday
difference,
for example you wants to take difference between current date (c_date
) and date of birth (dob
)date dob,c_date,today; if(c_date.dd>=dob.dd) today.dd = c_date.dd-dob.dd; else { c_date.dd+=30; c_date.mm-=1; today.dd = c_date.dd-dob.dd; } if(c_date.mm>=dob.mm) today.mm = c_date.mm-dob.mm; else { c_date.mm+=12; c_date.yy-=1; today.mm = c_date.dd-dob.mm; } today.yy = c_date.yy-dob.yy;
In
today
you have difference between two dates.There is one more way:
double difftime (time_t end, time_t beginning);
Read this answers:
1. How to compare two time stamp in format "Month Date hh:mm:ss"
2. How do you find the difference between two dates in hours, in C?这篇关于用C两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!