I am trying to determine the time difference between two times, which i represent as unsigned integers (in a sturct) as follows:

```
unsigned int day;
unsigned int month;
unsigned int year;
unsigned int hour;
unsigned int mins;
unsigned int seconds;
```

i can work out the time difference in minutes between two times that occur on the same day easily enough using: This isn't my exact code, this is just the logic behind it.

```
time1 = hours*3600 + mins*60 + seconds;
time1 = hours2*3600 + mins2*60 + seconds2;
//time2 will always be less than time1
time_diff_secs = time1_secs - time2_secs;
time_diff_mins = time_diff_secs / 60;
time_diff_secs = time_diff_secs % 60;
```

this produces this output:

```
Time mayday was issued: 13 Hours 4 Mins 0 Seconds
Time mayday was recieved: 13 Hours 10 Mins 0 Seconds
Time between sending and receiving: 6.00Mins
```

which is correct, but when I have two times that are on different days I get this as the result:

```
Time mayday was issued: 23 Hours 0 Mins 0 Seconds
Time mayday was recieved: 0 Hours 39 Mins 38 Seconds
Time between sending and receiving: 71581448.00Mins
```

This is obviously incorrect, I am not sure how to progress from here, the actual result should be 40mins, not 71.5million.

You are getting an underflow. Try this (works regardless of whether the variables are `signed`

or `unsigned`

):

```
if (time1_secs < time2_secs) {
// New day. Add 24 hours:
time_diff_secs = 24*60*60 + time1_secs - time2_secs;
} else {
time_diff_secs = time1_secs - time2_secs;
}
time_diff_mins = time_diff_secs / 60;
time_diff_secs = time_diff_secs % 60;
```

Change

```
time_diff_secs = time1_secs - time2_secs;
```

to

```
time_diff_secs = abs(time1_secs - time2_secs) % 86400;
```

This will force it to be the minimum time difference between both times and will work even if you add days, months, etcetera to the `time_diff_secs`

calculation.

Another way to do it using the standard C library, the only advantage of this is you do not have to worry about your dates overlapping years, or problems with overlapping month boundaries + leap year nonsense:

```
unsigned int day;
unsigned int month;
unsigned int year;
unsigned int hour;
unsigned int mins;
unsigned int seconds;
time_t conv(void)
{
time_t retval=0;
struct tm tm;
tm.tm_mday=day;
tm.tm_mon=month -1;
tm.tm_year=year - 1900;
tm.tm_hour=hour;
tm.tm_min=mins;
tm.tm_sec=seconds;
tm.tm_isdst=-1;
retval=mktime(&tm);
return retval;
}
int main()
{
time_t start=0;
time_t end=0;
time_t diff=0;
// assign day, month, year ... for date1
start=conv();
// assign day, month, year ... for date2
end=conv();
if(start>end)
diff=start - end;
else
diff=end - start;
printf("seconds difference = %ld\n", diff);
return 0;
}
```

Similar Questions

This is my code and it perfectly returns number of days and months between two dates. i want to validate this like the difference between two dates should be exactly one year.like if u select july 29

I want to get the difference between two Java Date objects. I've used Joda-Time library. But the problem is that I'm getting the Days greater difference than that of actual day difference. Here's my

I have been searching for this answer for days now and cannot find a simple solution. I have a simple form with two buttons JOB START & JOB FINISH. The button text equals the date/time Now o

This question already has an answer here: Calculate number of weekdays between two dates in Java 6 answers Calculating dates given two dates excluding weekend 3 answers Hi, i try this code

DateDiff() gives me the number of calendar dates between two dates. How can I get the number of working days (excluding Sat/Sun)?

I want to determine the number of days between two date/time fields : Start and Stop, but excluding the weekend days. So if Start = let say some date on Friday and Stop = the next Monday => the dif

I am trying to identify the duration of something by finding the difference between the start time and the end time. //bring date & time input into mysql datetime format for query $t_start_datetim

In JavaScript, how can I calculate the difference between two times that are in 24 hour format Example: Get how many hours elapsed from 08:00:00 to 23:00:00 Below I'm getting two time values from two

I want to calculate the distance between two points in two different datasets. I don't want to calculate the distance between all points - just to the nearest point of datasetB. Some examples: Dataset

I have a gridview as the below image: I have the textboxes in the ItemTemplate of the gridview. I want to calculate the number of days between the two dates and filled the last textbox with the value

How can I calculate the difference of two angle measures (given in degrees) in Java so the result is in the range [0°, 180°]? For example: 350° to 15° = 25° 250° to 190° = 60°

I work on a MySQL database table that has a column containing timestamps (Ex. 2014-09-16 09:08:05) of the time I ping different hosts. My question is how could I calculate in minutes the difference be

I am trying to calculate the displacement between two coordinates in meters, Is there a function in here maps that does this calculation? If not, how do I effectively calculate distance between the t

I am trying to calculate the difference between given two times and i find some problem in it. Suppose we have times value are 11:AM and 10:00 AM. I am able to calculate but there is a bit of confu

I have a DB sorted by date, and in my output, I would like to display the elapsed time between rows.. like this.. | Entry #1 on 08/10/2010 08:34 AM | Entry #2 on 08/10/2010 09:45 AM | 1 hour 11 minut

I'm trying to count the days between two dates, excluding Saturdays and Sundays. I've written this code so far Dim startDay As Integer Dim endDay As Integer Dim days As Integer Dim count As Integer st

I am trying to calculate how many days there are between different dates. Like the topic says, I'm getting a negative value. I guess I could just take the absolute value, but the fact that it returns

Possible Duplicate: How to find number of days between two dates using php Is there a quick and easy way to calculate the difference in days between two date strings in this format (YYYY-MM-DD) with

I need to calculate the number of days between 2 date. I have check the codes given this link How to calculate the number of days between two dates using JavaScript? . In this example if give input 20

Are there any classes/functions written in php publicly available that will take a timestamp, and return the time passed since then in number of days, months, years etc? Basically i want the same func

I want to calculate the difference between two dates and display it on the text box when TODATE is selected. Automatically difference between FROMDATE and TODATE will display. Note: Those calenders a

hey guys, I want to calculate the difference between two times and then compare difference is less than 5 MIN.. Please note I want difference in min. using c#.net

I'm new to excel and I would like some help on this. I have two spreadsheets. On the first sheet, I enter job information and in the second sheet, there is a list of total number of stocks correspond

I'm looking for a simple way to calculate the difference between two rectangles. I mean all points which belong to one of the rectangles, but not to both (so it's like XOR). The rectangles are axis-al

basically i have one time currentTime = DateFormat.getTimeInstance().format(new Date()); and i want to calculate how much time has past since currentTime. any ideas?

Hi friends this piece of code is not returning the correct time difference between two times instead returning NaN can any one spot the mistake in my code , thanks in advance . $([id^='endTime']).ch

I have two datepickers as follows : <input type=date name =datepicker_start id=datepicker value= class=datepicker size=20 /> and <input type=date name =datepicker_end id=dat

Is there any way to find Ascii difference between two strings in shell scripting.First characters should be checked initially and If first letters are same it should check for second of both. Just lik

I have a spreadsheet with data in Apple Numbers. I have a column of dates, and I would like to create a second column which displays the difference in number of days between the two dates. Example: Da

I'm trying to get the total number of days between two days: 1/1/2011 3/1/2011 RETURN 62 Is it possible to do in MS SQL?

In the question A very strange date before 1970, I know the reason why two times looks only have difference of 1s, but sometimes we can get another value. For example from that question: public static

i need to calculate distance in CGPoint between given two point(CGPoint) i refer this and this but i dont know how to get distance in CGPoint. so if any one knows Please help. Thanks.

This question already has an answer here: Get difference between 2 dates in javascript? 7 answers How to calculate the difference between two dates say date 1 is 28-04-2014 and date 2 is 30-04-

I need to calculate the duration between two datetimes in javascript.I have tried some code. var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy =

I have a requirement like calculating remaining days between two-dates. It is working fine for me. but the problem is when i sent the build to UK, there it is displaying one day less compare to India(

I want to get the difference between two times. - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setCurrentQuestion]; } -(void)setCurrentQuestion{ dateFormatter = [[NSDat

How could I determine the number of days between two NSDate values (taking into consideration time as well)? The NSDate values are in whatever form [NSDate date] takes. Specifically, when a user enter

I want to compute the similarity (distance) between two vectors: v1 <- c(1, 0.5, 0, 0.1) v2 <- c(0.7, 1, 0.2, 0.1) I just want to know if a package is available for calculating different well-k

I have been writing some queries about SQL Server in order to learn. I wonder that how can I get the difference of today between 90 days ago in a SQL query?

This question already has an answer here: How to calculate the difference between two dates using PHP? 26 answers I have two time values as follows $start = '13:00:00'; $end = '21:00:00'; I wa

I would like to know the difference between these two: Integer I = 30; // is it autoboxing? Integer I = new Integer(30); // I know it is autoboxing

Is it possible to use the mapkit in the iphone sdk to calculate driving distance between two addresses ?

I have an hour selection drop down 0-23 and minutes selection drop down 0-59 for Start time and End time respectively (so four controls). I'm looking for an algorithm to calculate time difference usin

I need to fetch a sub string that lies between two same or different delimiters. The delimiters will be occurring multiple times in the string, so i need to extract the sub-string that lies between mt

I want to calculate the number of years between two dates. One of them is retrieved from the database while the other is taken from user input in date format. I used this code to get the date from th

I have a taxi database with two datetime fields 'BookedDateTime' and 'PickupDateTime'. The customer needs to know the average waiting time from the time the taxi was booked to the time the driver actu

Im programming with PYTHON... I have two diferent dates...and I want to know the difference (in days) between them. The format of the date is YYYY-MM-DD To explain myself in a better way... I have a f

I would like to know some references and ideas in how to calculate the (set?) difference between two UML models. Basically, I have a system (represented in UML/xmi), and to this system several pattern

I am creating a Library Management System. I have used the timestamp to calculate the Date Difference and with the help of date difference I am calculating the Fine also. Now this date difference inc

How can I get the difference of days between two DateTime objects? private static string DaysAfterAYear(DateTime initialDate) { DateTime endDate = initialDate.AddYears(1); endDate = endDate.AddMonths(