473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

date difference problem

8 New Member
Hi guys, I have a problem to calculate difference in date using difftime.
Two strings are passed to the function day1="05/01/2007" and day2="04/30/2007" the difference I got is 2 days which is wrong since there is no 31st day in april. The most surprising part I can pass 04/31/2007 ang get the right difference without any error while creating struct tm. What am I doing wrong?
Here is my programm:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int DaysDifference(string const& day1, string const& day2){
  3.     int day,month,year;
  4.     double diff;
  5.  
  6.     day=StrToInt(day1.substr(3,2));
  7.     month=StrToInt(day1.substr(0,2));
  8.     year=StrToInt(day1.substr(6,4))-1900;
  9.     struct tm a= {0,0,12,day,month,year};
  10.  
  11.     day=StrToInt(day2.substr(3,2));
  12.     month=StrToInt(day2.substr(0,2));
  13.     year=StrToInt(day2.substr(6,4))-1900;
  14.     struct tm b= {0,0,12,day,month,year};
  15.  
  16.     time_t x = mktime(&a);
  17.     time_t y = mktime(&b);
  18.     if ( x != (time_t)(-1) && y != (time_t)(-1) ){
  19.         /*difftime returns difference in seconds*/
  20.         diff = difftime(x,y)/(60*60*24);
  21.     }
  22.  
  23.     return diff;
  24. }
  25.  
  26.  
Thank you for the help
Apr 15 '10 #1
1 2953
ilyaw77
8 New Member
Hey guys, I think I found my error, it should be:

Expand|Select|Wrap|Line Numbers
  1. month=StrToInt(day1.substr(0,2))-1;
  2.  
thnaks a lot though !!! :)
Apr 15 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
2
by: F | last post by:
Hi All I am facing the most common problem of date where system date is different and SQL server dateformat is different. How I can change Date fomat in SQL server from US to UK format vis...
7
by: Edward Mitchell | last post by:
I have a number of DateTimePicker controls, some set to dates, some set to a format of Time. The controls are all embedded in dialogs. I created the controls by dragging the DateTime picker from...
16
by: Atley | last post by:
I am trying to get a overall difference on two dates, I can get the difference in Years, Months, Weeks, Days, Hours, Minutes, Seconds, no problems... What I cannot seem to figure out is how to...
6
by: Scott | last post by:
I have a start date and an end date in my project that is defaulted to the current date at run-time using the following code... Date1.Value = Now() Date2.Value = Now() I then have some code...
13
by: priyasmita_guha | last post by:
Here is a program- /* PROGRAM: To find the difference between two dates */ #include<dos.h> #include<stdio.h> #include<conio.h> #include<process.h> void valid_date(int,int,int); int...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
1
by: Jim Slatter | last post by:
I am struggling with a simple task of finding the difference between the earliest date and latest date for records held in a sub-form. The dates are not necessarily always entered in the correct...
6
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.