number of days between 2 dates.

This is a discussion on number of days between 2 dates. within the C++ Programming forums, part of the General Programming Boards category; I want to be able to calculate the number of days between 2 dates. Ive looked at the boost c++ ...

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    number of days between 2 dates.

    I want to be able to calculate the number of days between 2 dates. Ive looked at the boost c++ libararies but it isnt possible for me me to use them as I have to take my code between multiple computers.

    Ive searched the forum and it returns this post:

    Calculating days between two Dates

    which talks about using difftime(); but I dont understand how that can work as Im trying to find days between dates not seconds between times

    Any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    86400
    Number of seconds in a day - what's the problem?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ok, i guess im being very stupid here but, both days will have the same number of second, giving me a differenence of 0 seconds

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    difftime returns the number of seconds between two times (time includes the date)

    So difference between Monday and Wednesday would be 172800 seconds.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    the thing is I have 2 dates, for example: 17 2 2005 and 14 2 2005

    each part stored as individual int's. I dont see how difftime() can work for this?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Well you store them both in separate struct tm variables.
    Then call mktime() to convert them both to time_t's
    then call difftime()

    I'm sure I've done this just recently - like say 5 seconds ago in another post.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Code:
    struct tm time_str;
        struct tm time_str2;
        long int diff = 0;
        time_str.tm_year = 2005-1900;
        time_str.tm_mon = 2-1;
        time_str.tm_mday = 4;
    
        time_str2.tm_year = 2005-1900;
        time_str2.tm_mon = 2-1;
        time_str2.tm_mday = 8;
    
        diff=difftime(mktime(&time_str2),mktime(&time_str));

    i get an answer of 0. any ideas why?
    Last edited by explosive; 02-17-2005 at 05:37 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Why does distributing your stuff to multiple computers prevent you from using Boost in any way? Distribute Boost as well, every computer with a C++ compiler should have it, anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    because some of the computers that I would need to use it on have restrictions in place so I cannot install it.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try zeroing out your time structs, otherwise you will get random values in the fields you don't fill in.
    Code:
    struct tm time_str  = { 0 };
    struct tm time_str2 = { 0 };

  11. #11
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    thanks! that fixed it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. Algorithm help
    By mmyers1 in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2004, 09:56 PM
  5. C++ number of days between 2 dates?
    By Rain1522 in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 08:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21