Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:23:02+00:00 2026-06-09T15:23:02+00:00

I need to be able to transform and compare dates in c++. I found

  • 0

I need to be able to transform and compare dates in c++. I found that the boost library pretty much suited my needs but I can´t get it to work correctly:

 // include headers... 

using namespace boost::posix_time; 
using namespace boost::gregorian;

ptime p1(second_clock::universal_time());

p1 -= weeks(5); // Subtract 5 weeks from p1 using boost...

std::string old_date("2011-11-19"); // format:YYYY-MM-DD

std:stringstream ss(old_date);

ptime p2;

p2 << ss;

if(p1 <= p2){
    // this should not happen in this case (yet it does!) p1 > p2!!
}

Basically I want to be able to subtract weeks (or months) of a local date and then compare the result with a date given as a string in YYYY-MM-DD format…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:23:03+00:00Added an answer on June 9, 2026 at 3:23 pm

    Your locale might not be set to recognize dates in the YYYY-MM-DD format. Try setting the input facet format as in the Format Strings example.

    Here’s an example that shows how to set the input and output formats of a stringstream to “ISO extended”:

    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    int main()
    {
        using namespace boost::gregorian;
        using namespace boost::posix_time;
        using namespace std;
    
        stringstream ss;
    
        /****** format strings ******/
        time_facet* output_facet = new time_facet();
        time_input_facet* input_facet = new time_input_facet();
        ss.imbue(locale(ss.getloc(), output_facet));
        ss.imbue(locale(ss.getloc(), input_facet));
        output_facet->format(time_facet::iso_time_format_extended_specifier);
        input_facet->format(time_input_facet::iso_time_format_extended_specifier);
        ptime t(second_clock::local_time());
        ss << t;
        cout << ss.str() << endl; // 2012-08-03 23:46:38
        ss.str("2000-01-31 12:34:56");
        ss >> t;
        assert(t.date().year() == 2000);
        assert(t.date().month() == 1);
        assert(t.date().day() == 31);
        assert(t.time_of_day().hours() == 12);
        assert(t.time_of_day().minutes() == 34);
        assert(t.time_of_day().seconds() == 56);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to output html controls using xslt, but I need to be able
I have a pretty simple LIKE search for MySQL that i'd like to transform
I need to be able to extract and transform data from a data source
I have a bunch of files that I need to be able to transport
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to access a list throughout my Rails project. For
I need to be able to search through multiple 2D arrays and search the
I need to be able to create an id for a hyperlink. I have
I need to be able to strip out a condition in a string, here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.