Hi, I would like to make a difference between two times and then save the result in SD card. For example, I have a start time (10:12:25) and an end time (11:30:12). i want the difference between end time and start time. I have an RTC module, a TFT LCD display from Adafruit and an Arduino Mega available. Is there a library for such calculation?


Is there a library for such calculation?

Some versions of the DateTime library have a function to convert a time/date to a Unix time. The difference between two Unix times will be the number of seconds between the two events.

I do not want to convert time but only a difference. I have already made a difference but the result is not 100% correct. The transmission from second to minute is not correct.

// Presst des Aktknopfs und Berechnung der Aktuelle laufzeit
     if (((x > 106) && (x <212)) && ((y > 200) && (y <240))) {
      sprintf(buf, "%02d:%02d:%02d", ((, ((, ((;

I do not want to convert time but only a difference.

I'm sorry, but that does not make sense.

You can calculate the time since midnight, for any time, if the date doesn't matter. Do that for each time, and you simply subtract one value from the other to get the difference.

unsigned long secondsSinceMidnight = ( * 24UL * 60 * 60) +
   ( * 60) +;

Now, if you want the interval in hours, minutes, and seconds, instead of just seconds, that is relatively easy to do.

By the way, I do NOT recommend getting the seconds since midnight the way I showed, because you do NOT want to call now() three times. You might NOT get the same time all three times.

Store the value returned by in the proper type variable, and get hour(), minute(), and second() from that variable.