输出日期下一天

本文介绍了一个使用C++实现的日期时间字符串到时间戳的转换方法,并演示了如何获取输入日期的下一天。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <sstream>
 4 
 5 using namespace std;
 6 
 7 int main(int argc, char **argv)
 8 {
 9     int ret(0);
10 
11     string line;
12     while (getline(cin, line))
13     {
14         int year(0), month(0), day(0), hour(0), minute(0), second(0);
15         ret = sscanf(line.c_str(), "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
16         if (6 != ret)
17         {
18             cout << "input date format invalid, please input like 2015-12-31 10:30:00" << endl;
19             continue;
20         }
21 
22         struct tm curTm;
23         curTm.tm_year = year - 1900;
24         curTm.tm_mon = month -1;
25         curTm.tm_mday = day;
26         curTm.tm_hour = hour;
27         curTm.tm_min = minute;
28         curTm.tm_sec = second;
29 
30         time_t curDaySec = mktime(&curTm);
31         time_t lastDaySec = curDaySec + (24 * 60 * 60);
32 
33         struct tm * lastTm;
34         lastTm = localtime(&lastDaySec);
35 
36         char result[100];
37         strftime(result, sizeof(result), "%F %T", lastTm);
38         cout << "result: " << result << endl;
39     }
40 
41     return 0;
42 }

 

转载于:https://www.cnblogs.com/itpoorman/p/5254476.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值