954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

date difference in c++

how to subtract two dates to find no of days .

animesh
Newbie Poster
7 posts since Jul 2004
Reputation Points: 11
Solved Threads: 0
 

i think that you can use

enum



it is as

structure

but enum list the elements by index as array .. for example[code]
enum days_of_the_week ={sat,sun,mon,tue,wed,thu,fri };
there for .. sat has the value 0 and sun = 1 and mon = 2 and so on ..
so if u say mon - sat
the compiler wil supstract the values only ... ther for u will get 2 .

meabed
Junior Poster
Team Colleague
139 posts since May 2004
Reputation Points: 55
Solved Threads: 3
 

This is one way.

<strong>#include</strong> <iostream>
<strong>#include</strong> <ctime> 
 
<strong>int</strong> <strong>main</strong>() 
{
<strong>   struct</strong> std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
<strong>   struct</strong> std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
   std::time_t x = std::<strong>mktime</strong>(&a);
   std::time_t y = std::<strong>mktime</strong>(&b);
<strong>   if </strong>( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
   {
	 <strong>double</strong> difference = std::<strong>difftime</strong>(y, x) / (60 * 60 * 24);
	 std::<strong>cout</strong> << std::ctime(&x);
	 std::<strong>cout</strong> << std::ctime(&y);
	 std::<strong>cout</strong> << "difference = " << difference << " days" << std::<strong>endl</strong>;
   }
<strong>   return</strong> 0; 
}
 
/* my output
Thu Jun 24 01:00:00 2004
Mon Jul 05 01:00:00 2004
difference = 11 days
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You