C++ compare to string dates(C++ 比较字符串日期)
问题描述
我需要比较 2 个字符串日期以查看一个日期是否晚于另一个日期.两个日期的日期格式都在底部.我可以重新安排这个最简单的.我有提升,但它不一定是,我经历了这么多例子,似乎无法让我的大脑围绕让它工作.提前谢谢,基本上我想要
I need to compare 2 string dates to see if one date is later then another. The date format for both dates is at the bottom. i can rearrange this for what ever is easiest. I have boost but it doesn't have to be, ive been through so many examples and can't seem to wrap my brain around getting it to work. Thanks in advance basically i want
2012-12-06 14:28:51
2012-12-06 14:28:51
if (date1 < date2) {
// do this
}
else {
// do that
}
推荐答案
看起来您使用的日期格式已经按字典顺序排列,并且可以进行标准字符串比较,例如:
It looks like the date format your using is already in lexicographical order and a standard string comparison will work, something like:
std::string date1 = "2012-12-06 14:28:51";
std::string date2 = "2012-12-06 14:28:52";
if (date1 < date2) {
// ...
}
else {
// ...
}
使用此格式时,您需要确保间距和标点符号一致,尤其是像 2012-12-06 9:28:51 这样的内容会破坏比较.2012-12-06 09:28:51 会起作用.
You will need to make sure that spacing and punctuation is consistent when using this format, in particular something like 2012-12-06 9:28:51 will break the comparison. 2012-12-06 09:28:51 will work though.
这篇关于C++ 比较字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 比较字符串日期
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
