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
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01