Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get difference between 2 date i.e. hire date and joining date in c sharp.
Posted

1 solution

If you are using MFC then you can use:


int GetDays( int endYear, int endMonth, int endDay,
                    int begYear, int begMonth, int begDay )
{        
    CTime beg(begYear, begMonth, begDay, 0, 0,0 );
    CTime end(endYear, endMonth, endDay, 0, 0,0 );

    CTimeSpan dist = end - beg;

    return dist.GetDays();
}


or otherwise, this could help you:

struct tm date1;
struct tm date2;

memset ( &date1, 0, sizeof ( struct tm));
memset ( &date2, 0, sizeof ( struct tm));

date1.tm_mday = 13;
date1.tm_month = 6;
date1.tm_year = 2003;

date2.tm_mday = 14;
date2.tm_month = 3;
date2.tm_year = 2004;

time_t time1, time2;

time1 = mktime ( &date1);
time2 = mktime ( &date2);

double dSecs = difftime ( time2, time1);

double dDays = dSecs / ( 3600.0 * 24);


Hope this helps
 
Share this answer
 
Comments
Peter_in_2780 20-Jun-11 8:05am    
If you use struct tm, be sure to remember that tm_month runs 0..11, not 1..12! A trap that has caught many a good coder. Also, check the definition of tm_year. It's not what you think it is!
@nuraGGupta@ 20-Jun-11 8:25am    
Didn't reviewed the code in editor, just posted it so that OP may have some idea of how to perform the task.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900