C ++比较字符串日期 [英] C++ compare to string dates

查看:211
本文介绍了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 ++比较字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆