如何从日期中减去时间
How to subtract time from date
是否有标准的c++时间类有方法来减去日期例如:我想从这样的日期(Jan 1 Mon 2014 00:05)中减去10分钟,得到这样的日期(Dec 31 Sun 2013 23:55)
如果"struct tm"类型有这样的函数就好了
cppreference显示了足够的函数(mktime, localtime)来将struct tm
转换为time_t
。time_t
的计算很明显,因为它是一个整数类型,传统上表示1970年以来的秒数。
我不知道struct tm
是否有任何方法,但我认为你应该阅读C++11
中可用的chrono
。链接
如果你会使用c++ 11,你可以很容易地减去和添加日期:
#include <iostream>
#include <chrono>
#include <ctime>
#include <ratio>
using namespace std;
int main ()
{
chrono::system_clock::time_point today = chrono::system_clock::now();
chrono::system_clock::duration dtn (chrono::duration<int,ratio<60,1>>(10));
time_t t = chrono::system_clock::to_time_t(today-dtn);
cout << ctime(&t) << endl;
return 0;
}
相关文章:
- 使用C++的日期库读取时间
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- 为什么在C++中获取日期和/或时间如此复杂?
- 在 MacOS 上C++:显示日期和时间问题
- 如何将我的每个矢量字符串转换为纪元时间日期
- 将 unix 时间戳转换为人类可读的日期
- 使用霍华德·欣南特的日期库将时间信息添加到year_month_day
- 注册表-获取值(而不是键)上次更改的时间和键创建日期
- 我的C++程序如何访问计算机的日期和时间以打印和使用当前年份?
- 使用 C++将时间戳转换为格式化的日期时间
- C++代码的 C# 日期时间等效项是什么?
- 以毫秒级精度对日期和时间进行操作
- 如何在提升日期时间中创建日期范围?
- 将日期字符串转换为 unix 时间戳
- C 自定义时间日期结构到UTC时期
- 使用SNTP(Windows C )从服务器获取时间/日期
- C 的SNTP客户端的时间/日期
- 将文件名的文件另存为当前时间日期
- 在没有宏的情况下获取编译时间日期和时间
- C++应用程序构建时间/日期保存