#include <iostream> #include <chrono> #include <ctime> #include <iomanip> #include <sstream> int main() { // 获取当前时间点 auto now = std::chrono::system_clock::now(); // 计算1.5小时前的时间点 auto oneAndHalfHoursAgo = now - std::chrono::hours(1) - std::chrono::minutes(30); // 将时间点转换为time_t以便格式化输出 std::time_t time_t_now = std::chrono::system_clock::to_time_t(now); std::time_t time_t_oneAndHalfHoursAgo = std::chrono::system_clock::to_time_t(oneAndHalfHoursAgo); // 格式化输出 char buffer[80]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&time_t_now)); std::cout << "当前时间: " << buffer << std::endl; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&time_t_oneAndHalfHoursAgo)); std::cout << "1.5小时前: " << buffer << std::endl; return 0; }
标签:std,1.5,include,chrono,代码,获取,time,now From: https://www.cnblogs.com/leochan007/p/18688121