如何比较C++中的两个日期
How to compare two dates in C++
我需要比较两个日期。用户输入 1 的当前日期,格式为 dd/mm/yyyy。
所以如果expiry_date> current_date
显示。。。。我尝试过 difftime(),但这效果不佳,因为它使用了时间参数
time_t now;
struct tm newyear;
double seconds;
time(&now); /* get current time; same as: now = time(NULL) */
newyear = *localtime(&now);
newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
newyear.tm_mon = 0; newyear.tm_mday = 1;
seconds = difftime(now,mktime(&newyear));
printf ("%.f seconds diff", seconds);
system("pause");
这是我找到的示例代码
如果这真的是一个固定的格式,你可以通过简单的C字符串比较来完成
int date_cmp(const char *d1, const char *d2)
{
int rc;
// compare years
rc = strncmp(d1 + 6, d2 + 6, 4);
if (rc != 0)
return rc;
// compare months
rc = strncmp(d1 + 3, d2 + 3, 2);
if (rc != 0)
return rc;
// compare days
return strncmp(d1, d2, 2);
}
尝试 strftime 将字符串解析为 tm*,然后使用 mktime() 和 difftime()。
相关文章:
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- CPP 程序二查找两个给定日期之间的天数,conol CMD
- 解决方案在两个日期之间的天数内不起作用
- 使用 c++ 获取两个日期与时间字符串之间的差异(以秒为单位)
- C 的两个日期之间的差异
- 如何使用C Win32 API计算两个日期之间的天数
- 连接两个time_t值的日期和时间
- 如何比较C++中的两个日期
- 在C++中添加两个日期
- 两个日期之间的 C++ 日期差异
- 两个对象之间的日期比较
- visual如何在C++中比较两个日期
- 两个日期之间的总天数
- 两个日期之间的秒差
- 用c++计算两个日期的差异
- QDate -在两个日期之间搜索
- 如何比较程序中的两个日期
- C++-比较两个日期
- 比较来自字符串、char*或int的两个日期
- 如何使用boost::date_time获取两个日期之间的天数