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

  • SEARCH
  • Home
  • 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 6924505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:40:17+00:00 2026-05-27T10:40:17+00:00

This is how im generating my date i want to add 1 year to

  • 0

This is how im generating my date i want to add 1 year to it. Thanks in advance.

char tmpbuf[128];
time_t ltime;
struct tm *today;
stringstream reD;
string todayDate;
time( &ltime );
today = localtime( &ltime );
strftime( tmpbuf, 128,"%Y-%m-%d_%H:%M:%S", today );
reD << tmpbuf;
reD >> todayDate;
boost::replace_all(todayDate, "_", " ");
cout << todayDate << endl;

OK ive decided to go with boost since it will be easier to add days, so 2 examples i need one to add 1 year, and one to add 14 days, heres what i have so fare

 #include "boost/date_time.hpp"
 #include "boost/date_time/local_time/local_time.hpp"

 using namespace boost::posix_time;
 using namespace boost::local_time;

 int main(){
    local_date_time t = local_sec_clock::local_time(time_zone_ptr());
    local_time_facet* lf(new local_time_facet("%Y-%m-%d_%H:%M:%S"));
    std::cout.imbue(std::locale(std::cout.getloc(), lf));
    std::cout << t << std::endl;
    return 0;
 }

Edit putting time into string

 stringstream reD;
 reD.imbue(locale(reD.getloc(), lf));
 reD << t;
 bthis = reD.str();
 cout << bthis << endl;
  • 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-05-27T10:40:18+00:00Added an answer on May 27, 2026 at 10:40 am

    I agree about using boost::date_time, however, the solution here is quite easy.

    today->tm_year++;
    

    Although, if you happen to call localtime again, the value will be overwritten, so you should make a copy. Make today an instance instead of a pointer, and dereference the return value of localtime like this:

    today = *localtime( &ltime );
    

    You’ll have to take into account certain anomalies, like incrementing a year from February 29th on a leap year.

    Edit: I see you’ve decided to use boost::date_time after all. This makes things much simpler. Here’s how you add a year:

    t += boost::gregorian::years(1);
    

    And here’s how you add 14 days:

    t += boost::gregorian::days(14);
    

    Or

    t += boost::gregorian::weeks(2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm generating buttons dynamically that I want it to call a method like this:
I have a route defined like this: routes.MapRoute(Date, Date/{year}/{month}/{day}, new { controller = Date,
URL u=new URL("telnet://route-server.exodus.net"); This line is generating : java.net.MalformedURLException: unknown protocol: telnet And I
This code works great for generating thumbnails, but when given a very large (100MB+)
I am generating sprites programatically like this: this.food = new Sprite(); this.food.graphics.beginFill(0xFFFFFF); this.food.graphics.drawRect(0, 0,
I was facing this unique problem of generating a bit-mask based on the input
I'm generating a string of values (colors) like so: 'red|green|blue|yellow|orange|black' Can I pass this
I'm generating auto incremental code through a PHP script. The sequence goes like this,
I'm generating an HTML file for an app, and in this HTML file there's
I am generating a byte array of size about 250MB. This is then being

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.