Thread: Adding #days to a date

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Adding #days to a date

    So far I have this bit of code for adding number of days to a date using the time.h module:
    Code:
    int main(int argc, char **argv)
    {
    	int numDays;
    	printf ("Enter number of days to add ");
    	scanf ("%d", &numDays);
    	time_t now = time(NULL);
       
    	struct tm* tm = localtime(&now);
       
    	tm->tm_mday += numDays;
       
    	cout << asctime(tm);  
    	
    	return 0;
    }
    This works ok until you add 30 or more days to the date, then it'll give me something like>>September 53 2011<< if I add 40 days .

    What have I done wrong??
    Last edited by acidblue; 09-13-2011 at 08:48 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use mktime to normalise the values, so Sept 31 would become Oct 1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This looks like C and not C++, so this thread is in the wrong section. For what reason did you put it here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Quote Originally Posted by Salem View Post
    Use mktime to normalise the values, so Sept 31 would become Oct 1.
    I've tried:
    Code:
    cout << mktime(tm);
    But that gives the same results

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Are you sure? mktime returns the day expressed as seconds from the epoch. You would have to take that result and make it into a string again with something like ctime().

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    umm.....
    Code:
    #include <iostream>
    #include <ctime>
    
    int main(void){
    
    	tm *tmptr = NULL;
    	time_t now = time(NULL);
    	int numdays;
    
    	tmptr = localtime(&now);
    
    	std::cout << "Current date/time is: "<<asctime(tmptr)<<std::endl;
    	std::cout << "Enter number of days to add: ";
    	std::cin >> numdays;
    
    	tmptr->tm_mday += numdays; //add number of days
    	mktime(tmptr);//normalize time ptr
    
    	std::cout << numdays << " days later will be: "<<asctime(tmptr)<<std::endl;
    	
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Thanks AndrewHunter your example works.
    Strange though all I changed was this:
    Code:
    struct tm* tm = localtime(&now);
       
    	tm->tm_mday += numDays;
            mktime(tm);
    To this:
    Code:
    tmptr = localtime(&now);
       
    	tmptr->tm_mday += numDays;
    	mktime(tmptr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Date in a program
    By Nisheeth in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2011, 10:20 AM
  2. Adding to a string date
    By knutso in forum C Programming
    Replies: 11
    Last Post: 01-20-2004, 11:18 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. current date - 3 days ago
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 03:21 PM
  5. How to add Days to a Date?
    By Bufferlo in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 10:19 AM