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++ 比较字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!