2

I have been having a hard time solving this, maybe someone can help or provide suggestions. So here is the problem. I have Month, Day, and Year stored as int. I want to be able to add a fixed number of days to a date and get a new date. How would I go about doing this?

T.Malo
  • 512
  • 1
  • 7
  • 24
  • 3
    please add a MCVE ttp://stackoverflow.com/help/mcve – Chantola Sep 25 '14 at 02:45
  • Pls read up on the date libraries in thr stl – Ed Heal Sep 25 '14 at 02:47
  • 1
    Start here: http://en.cppreference.com/w/cpp/chrono/c and here: http://en.cppreference.com/w/cpp/chrono – Mark Ransom Sep 25 '14 at 02:53
  • You probably want [`mktime`](http://linux.die.net/man/3/mktime). Take particular note of the fact that it normalizes the members of the struct you pass, so if you just add the number of days to the date you're given it will convert your (say) 40th of February into the twelfth of March (or eleventh, in a leap year). – Jerry Coffin Sep 25 '14 at 03:14
  • Why this question has negative votes??? Upvoting. – sfelber Sep 25 '14 at 03:24
  • Handling calendar and time information is not trivial. Please use an existing library if possible of at least steal a working algorithm from a library uless you are doing For Academic Purposes. – jkj Sep 25 '14 at 03:24
  • 1
    @stviper I quess Date Algorihms make less smart peoples heads hurt too much not to downvote. Upvoting to compensate. – jkj Sep 25 '14 at 03:26
  • 1
    Possible duplicate of [Algorithm to add or subtract days from a date?](http://stackoverflow.com/questions/2344330/algorithm-to-add-or-subtract-days-from-a-date) – jww Sep 25 '14 at 06:19

2 Answers2

1

Supposing you're doing it for learning (there are many date libraries out there)... what is the exact problem you have? The algorithm is not very complex indeed and sounds like:

  1. add the number of days to the day field of the date
  2. if the number of days is now bigger than the maximum allowed (e.g. your date is like april 134, 2014) then decrement the number of days with the number of days of the month and increment the month number: e.g. april has 30 days, so date becomes may 104 2014
  3. repeat step 2 until the number of days is ok for the month (june 73, july 43, august 12)

the tricky parts are that

  1. the number of days of february depends on the year with a strange rule (it's 29 if year%4==0 && (year%100!=0 || year%400==0), 28 otherwise).
  2. when incrementing the month you may get past december, in that case go back to january but increment the year

This is not the fastest approach (requires you to loop over the months) but it's not difficult to implement.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Yes, thanks this is something I was thinking too. I was working with time.h and can't seem to find or use any functions that would do this. So I was thinking it had to be done like that. I will try to see if I can get that to work. – T.Malo Sep 25 '14 at 13:59
  • 1
    If you use `` the problem is trivial. The function `mktime` given a day/month/year will give you the number of seconds since 1/1/1970: add to it the number of days times 86400 (60*60*24 ... number of seconds in a day) and then call `localtime` to convert back to a structure with day/month/year. – 6502 Sep 25 '14 at 14:25
  • What, you can do that!? That is amazing! :) I just finished working out the nested logic to add days to get a new date like you suggested and it works. I am going to try to do it that way to. Any C++/C book recommendations? – T.Malo Sep 25 '14 at 14:33
0

Use boost date_time:

date d1(2014, 9, 25);
date_duration dd(10);
date d2 = d1 + dd;

This calculates the date 10 days after September 25, 2014. I don't think it gets any better than that.

Nir Friedman
  • 17,108
  • 2
  • 44
  • 72