How to subtract 30 days from a current Date in c++ using poco library?

烂漫一生 提交于 2021-01-29 05:18:21

问题


How to subtract 30 days from a current Date in c++ using poco library ?


回答1:


First approach is to substract to the date a new DateTime constructed like this:

Poco::DateTime(0, 0, 30);

But it is not allowed at runtime because month must be greater than 1. The solution is to use a Timespan:

Poco::DateTime date = Poco::DateTime();
std::cout << Poco::DateTimeFormatter::format(date, Poco::DateTimeFormat::ASCTIME_FORMAT) << std::endl;
date = Poco::DateTime(date.timestamp() - Poco::Timespan(30 * 24 * 60 * 60, 0)); // 30 days in seconds;
std::cout << Poco::DateTimeFormatter::format(date, Poco::DateTimeFormat::ASCTIME_FORMAT) << std::endl;

This is the output of this code snippet:

Fri Sep 18 08:18:21 2020
Wed Aug 19 08:18:21 2020


来源:https://stackoverflow.com/questions/63940303/how-to-subtract-30-days-from-a-current-date-in-c-using-poco-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!