c++日期差异
C++ date difference-
我有字符串格式的日期-例如-
string date1= "06/12/2014"
string date2= "06/12/2015"
现在我的要求是找出这两个日期之间的差天数(date2-date1) -
必须在c++代码中使用-
任何快速的帮助将是感激的-谢谢
标准库做得很好
阅读手册
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <memory>
using namespace std;
void toTM(string date, struct tm *t) {
stringstream ss(date);
string month, day, year;
getline(ss, month, '/');
getline(ss, day, '/');
getline(ss, year, '/');
memset(t, 0, sizeof(struct tm));
t->tm_mday = stoi(day);
t->tm_mon = stoi(month) - 1;
t->tm_year = stoi(year) - 1900;
}
int main(int argc, char** argv) {
string date1 = "06/12/2014";
string date2 = "06/12/2015";
struct tm time1, time2;
toTM(date1, &time1);
toTM(date2, &time2);
int diff = difftime(mktime(&time2), mktime(&time1));
cout << diff / 60 / 60 / 24 << " days" << endl;
return EXIT_SUCCESS;
}
相关文章:
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- Log4cpp:以UTC/GMT时区打印日期
- 使用C++的日期库读取时间
- 日期格式为C++
- 如何在 C++20 计时中为日期添加天数?
- 如何在QTableView中排序和更改日期格式
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- fmt 与 Howard Hinnant 的日期:为什么从 fmt::to_string "{}"?FMT 和日期的最佳实践?
- 使程序检查当前日期是否=预期日期
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 确定夏令时是否适用于特定日期
- C++日期库因时区而失败
- 向日期运算符添加天数+
- 为什么在C++中获取日期和/或时间如此复杂?
- 如何验证用户输入月份的日期?
- 从编译时已知的日历日期创建"std::chrono::time_point"
- 在 MacOS 上C++:显示日期和时间问题