Calculate the number of Days between two dates

This is a discussion on Calculate the number of Days between two dates within the C Programming forums, part of the General Programming Boards category; Ok, my book said I can use the following formula to calculate the days between two dates. N = 1461 ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    Calculate the number of Days between two dates

    Ok, my book said I can use the following formula to calculate the days between two dates.

    N = 1461 x f(year, month) / 4 + 153 g(month /5 + day

    where

    f(year,month) = year-1 if month <=2
    year otherwise

    g(month) = month + 13 if month <=2
    month +1 otherwise

    But then it saids this formula only works for dates after March1, 1900( 1 must be added for dates form March 1, 1800 to February 28, 1900 and2 must be added for dates between March 1, 1700 to February 28, 1800

    Now I have to write a program that permits the user to typein two dates and then calculates the number of elapsed days.

    Ok this is what I have so far.
    Code:
    #include <stdio.h>
    Code:
    
    int main () {
     
    struct date {
     int month; 
    int day; 
    int year;
     };
     
    struct date d1, d2, N, N1, N2, x;
     int daynumber(struct date d);
     int elapsedDays;
     int days_between(struct date d1, struct date d2);
     
    printf ("Enter date 1 (MM/DD/YYYY): ");
     scanf ("%d/%d/%d", &d1.month, &d1.day, &d1.year);
     printF("\n");
     
    if ( d1.month <= 2 ) 
    //suppose to be something with f(year, month).
     // if( month <= 2){
     // f(year, month) = year - 1; g(month) = month + 13;
     { 
    int year = d1.year - 1;
     int month = d1.month + 13;
     }
     else
     {
     year = d1.year;
     month = d1.month + 1;
     }
     
    printf ("Enter date 2 (MM/DD/YYYY): ");
     scanf ("%d/%d/%d", &d2.month, &d2.day, &d2.year);
     printF("\n");
     
    if ( d2.month <= 2 ) 
    { 
    year = d2.year - 1;
     month = d2.month + 13;
     ?}
     else 
    { 
    year = d2.year;
     month = d2.month + 1;
     }
     if( d1 >= 03/01/1800 && d1 <= 02/28/1900)
     {
     N = N+1;
     }
     else if( d1 >= 03/01/1700 && d1 <= 02/28/1800)
     {
     N = N+2;
     }
     if( d2 >= 03/01/1800 && d2 <= 02/28/1900)
     {
     N = N+1;
     }
     else if( d2 >= 03/01/1700 && d2 <= 02/28/1800)
     {
     N = N+2;
     }
     // Computes N
     int daynumber(struct date d)
     {
     N1 = 1461 * f(d1.year,d1.month) / 4 + 153 * g(d1.month) / 5 + d1.day;
     N2 = 1461 * f(d2.year,d2.month) / 4 + 153 * g(d2.month) / 5 + d2.day;
     }
     
    // Computes elapsed days between two dates
     int days_between(struct date d1, struct date d2)
     {
     elapsedDays = N2 - N1;
     
    return elapsedDays;
     }
     
    // Function to determine if date d is before date x
     int isbefore(struct date d, struct date x); 
    int isbefore(struct date d, struct date x)
     {
     
    if ( d < x ) 
    x = 1; 
    else
     x = 0; 
    return x;
     }
     
    printf ("There are %d days between %d/%d/%d and %d/%d/%d", 
    elapsedDays, d1.month, d1.day, d1.year, d2.month, d2.day, d2.year);
     printF("\n");
     
    return 0; 
    }
    

    I can not get pass compiling the Program. I pretty new at this and can not figure out my mistakes
    Any help will be appreciated, Thanks, Bill

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    Uh, did you do something funky with the font? It makes your code look unreadable.

    Anyway, your problem appears to be one of formatting. Indent your code properly and you should be able to find that you defined your struct in main unintentionally, and then you have mismatched braces.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-23-2011, 09:24 PM
  2. how can i get the number of days between two dates?
    By acohrockz21 in forum C Programming
    Replies: 3
    Last Post: 02-29-2008, 09:43 AM
  3. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 06:30 AM
  4. days between dates formula
    By maes in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-12-2004, 12:52 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