The following program will gives difference between two dates (time duration).
Note: compile using g++ filename.cpp -lboost_date_time
Function which returns number working days between 2 dates
If you want to calculate number of working days between two specific dates you can use the given function. It will ignore all Saturday and Sunday during this period (may useful in financial field).
#include <iostream> #include <boost/date_time/gregorian/gregorian.hpp> #include "boost/date_time/posix_time/posix_time.hpp" int main() { std::string date_1 = "2016-05-15 10:12:02"; std::string date_2 = "2016-09-15 16:40:01"; boost::posix_time::ptime t1(boost::posix_time::time_from_string(date_1)); boost::posix_time::ptime t2(boost::posix_time::time_from_string(date_2)); boost::posix_time::time_duration td = t2 - t1; std::cout <<"difference between dates :" << boost::posix_time::to_simple_string(td) << std::endl; }
Note: compile using g++ filename.cpp -lboost_date_time
Function which returns number working days between 2 dates
If you want to calculate number of working days between two specific dates you can use the given function. It will ignore all Saturday and Sunday during this period (may useful in financial field).
int dateDifference(boost::gregorian::date startDate_, boost::gregorian::date endDate_) { int dayCount=0; for(boost::gregorian::day_iterator iter = startDate_; iter!= endDate_; ++iter) { if(iter->day_of_week() != boost::date_time::Saturday && iter->day_of_week() != boost::date_time::Sunday) ++dayCount; } return dayCount; }
Good job man. Please give some comments it would be useful to understand for newbies.
ReplyDeletecan i get diiference of only time (H:M:S) without using date ?
ReplyDelete