CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Posts
    39

    How to find difference between two dates dd/mm/yyyy?

    Title ???

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How to find difference between two dates dd/mm/yyyy?

    Code:
    COleDateTime t1(2005,3,17,0,0,0);
    COleDateTime t2(2004,3,1,0,0,0);
    
    COleDateTimeSpan ts = t1 - t2;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Nov 2004
    Posts
    67

    Re: How to find difference between two dates dd/mm/yyyy?

    difftime: Computes the difference between two times.

    mktime: Converts time to calendar format.

  4. #4
    Join Date
    Feb 2005
    Posts
    39

    Re: How to find difference between two dates dd/mm/yyyy?

    Thanks,

    i use it but to separate dates i make this
    Code:
    int HelpingHand::DifferenceBetweenDates(char *date1,char *date2){
    
    	int day1,month1,year1,day2,month2,year2;
    	int totalsep = 0, i = 0, j = 0 ;
    	char num[4];
    	
    	ZeroMemory(num, sizeof(num));	
    	while(date1[i] != '\0'){
    		TRACE("%c",date1[i]);
    		if ( totalsep == 0 && (date1[i] == '-' || date1[i] == '/')){
    			day1 = atoi(num);
    			ZeroMemory(num, sizeof(num));
    			totalsep += 1;
    			j=0;
    			i++;
    			continue;
    		}else if(totalsep == 1 && (date1[i] == '-' || date1[i] == '/')){
    			month1 = atoi(num);
    			ZeroMemory(num, sizeof(num));
    			totalsep += 1;
    			j=0;
    			i++;
    			continue;
    		}
    		num[j] = date1[i];
    		i++;
    		j++;
    	}
    	year1 = atoi(num);
    	ZeroMemory(num, sizeof(num));	
    
    	totalsep = 0;
    	i=0;
    	j=0;
    	TRACE("\n");
    	while(date2[i] != '\0'){
    		TRACE("%c",date2[i]);
    		if ( totalsep == 0 && (date2[i] == '-' || date2[i] == '/')){
    			day2 = atoi(num);
    			ZeroMemory(num, sizeof(num));
    			totalsep += 1;
    			j=0;
    			i++;
    			continue;
    		}else if(totalsep == 1 && (date2[i] == '-' || date2[i] == '/')){
    			month2 = atoi(num);
    			ZeroMemory(num, sizeof(num));
    			totalsep += 1;
    			j=0;
    			i++;
    			continue;
    		}
    		num[j] = date2[i];
    		i++;
    		j++;
    	}
    	year2 = atoi(num);
    	ZeroMemory(num, sizeof(num));	
    	
    	
    	COleDateTime t1(year1,month1,day1,0,0,0);
    	COleDateTime t2(year2,month2,day2,0,0,0);
    
    	COleDateTimeSpan ts = t2 - t1;
    
    return ts;
    
    }
    do you can know more simple . Thanks again.

    difftime: Computes the difference between two times.

    mktime: Converts time to calendar format.
    Time? Data....

  5. #5
    Join Date
    Feb 2005
    Posts
    39

    Re: How to find difference between two dates dd/mm/yyyy?

    In another form....

    date if give like:
    dfCreated = 16-03-2005 11:17:28
    dNow = 17-03-2005 18:41:20

    i try to use this...
    Code:
    int HelpingHand::DifferenceBetweenDates(char *dfCreated,char *dNow){
    
    	CTime diff = (dNow - dfCreated);
    
    	CString stemp = diff.Format( "Total days: %D, hours: %H, mins: %M, secs: %S" );
    ...
    output = Total days: , hours: 00, mins: 01, secs: 12 !!!!!!!


    sorry the confusion...

  6. #6
    Join Date
    Nov 2004
    Posts
    67

    Re: How to find difference between two dates dd/mm/yyyy?

    What I had in mind was more like...

    #include <stdio.h>
    #include <time.h>

    #define EXTRACT_HOUR(sec) ((sec)/(60*60))

    int main(void)
    {
    time_t t1, t2, t3;
    struct tm datetime;

    datetime.tm_year = 2005 - 1900;
    datetime.tm_mon = 03 - 1;
    datetime.tm_mday = 17;
    datetime.tm_hour = 1;
    datetime.tm_min = 2;
    datetime.tm_sec = 3;
    datetime.tm_isdst = 0;
    t1 = mktime(&datetime);
    printf("Date and time #1: %s\n", ctime(&t1));

    datetime.tm_year = 2005 - 1900;
    datetime.tm_mon = 03 - 1;
    datetime.tm_mday = 17;
    datetime.tm_hour = 8;
    datetime.tm_min = 7;
    datetime.tm_sec = 6;
    datetime.tm_isdst = 0;
    t2 = mktime(&datetime);
    printf("Date and time #2: %s\n", ctime(&t1));

    t3 = t2-t1;
    printf("Total number of seconds : %ld \n", t3);
    printf("Relative number of hours is: %ld heures\n", EXTRACT_HOUR(t3) );

    return 0;
    }

    I dont know any shorter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured