Calculating time elapsed between two dates

Hello,

I am working on a project, where I use the DS1307 RTC module. To be specific, I have a date for example 2020.07.10. 14:40. What I’m trying to achieve, is to display the difference between the current date and that example one. So far, I have used now.unixtime() and subtracted a constant from it. This worked perfectly with days. The constant was the elapsed seconds between now.unixtime() and the example date.

unsigned long result = now.unixtime() - diff;

I wanted to display the result in days, months and years. (YY:MM:DD) Days are easy to work with as I only have to divide with 86400. But because months can be 28, 29, 30, or 31 days I was faced with this problem. Fortunately, I figured out something: I count the days from 0 to the current day, then at a certain point let’s say 31, I increment the month counter by 1. After that, I add the next month’s number to the previous one and so on… till I reach 12 where I close the loop. It is getting kinda complicated so I attach the code to this topic.

I used a for loop, but I think somethin went wrong there, as the “honap” variable overflows rapidly, and if I only execute the for loop when the day changes, that variable counts everything, and I does not work as it supposed to do.

#include <RTClib.h>
#include <Wire.h>
RTC_DS1307 rtc;
unsigned int prevv;
boolean happened = false;
const unsigned long diff = 1598063880;
int honap;
int kamunap;
unsigned long ev;
int prevday;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  rtc.adjust(DateTime(2020, 11, 10, 2, 37, 50));
}
void loop() {
  //  if (beallitobanvan1 == true) setora();
  DateTime now = rtc.now();
  unsigned long result = now.unixtime() - diff;
  unsigned int nap = result / 86400;
  int honapszam = now.month();
  int evszam = now.year();
  kamunap = nap;
  if (prevday != nap) happened = true;
  if (happened == false) {
    for (int i = 0; i < nap; i++) {
      if (i < 31) kamunap = nap;
      if (i = 31) {
        honap++;
        kamunap = 0;
      }
      if (31 < i < 61) kamunap = nap - 31;
      if (i = 61) {
        honap++;

        kamunap = 0;
      }
      if (61 < i < 92) kamunap = nap - 61;
      if (i = 92) {
        honap++;

        kamunap = 0;
      }
      if (92 < i < 122) kamunap = nap - 92;
      if (i = 122) {
        honap++;

        kamunap = 0;
      }
      if (122 < i < 153) kamunap = nap - 122;
      if (i = 153) {
        honap++;

        kamunap = 0;
      }
      if (153 < i < 184) kamunap = nap - 153;
      if (i = 184) {
        honap++;

        kamunap = 0;
      }
      if (184 < i < 213) kamunap = nap - 184;
      if (i = 213) {
        honap++;
        kamunap = 0;
      }
      if (213 < i < 244) kamunap = nap - 213;
      if (i = 244) {
        honap++;

        kamunap = 0;
      }
      if (244 < i < 274) kamunap = nap - 244;
      if (i = 274) {
        honap++;

        kamunap = 0;
      }
      if (274 < i < 305) kamunap = nap - 274;
      if (i = 305) {
        honap++;

        kamunap = 0;
      }
      if (305 < i < 335) kamunap = nap - 305;
      if (i = 335) {
        honap++;

        kamunap = 0;
      }
      if (335 < i < 366) kamunap = nap - 335;
      if (i = 366) {
        honap++;

        ev++;
        kamunap = 0;
      }
    }
    happened = true;
    prevday = nap;
  }
  int yy = honap / 12;
  int mm = honap % 12;
  int dd = kamunap;
}

The time library (TimeLib.h) makes this simple. Convert any pair of unix times to dates and do the subtractions.

That’s a brilliant library for that.

#include <TimeLib.h>
void setup() {
  // put your setup code here, to run once:


  time_t t = 1598063880; //unix timestamp
  setTime(t);
  Serial.print("Date: ");
  Serial.print(day());
  Serial.print("/");
  Serial.print(month());
  Serial.print("/");
  Serial.print(year());
  Serial.print(" ");
  Serial.print(hour());
   Serial.print(":");
  Serial.print(minute());
 Serial.print(":");
  Serial.println(second()); 
}

void loop() {
  // put your main code here, to run repeatedly:
}

So this code works perfectly. However, it sets the date into the RTC, and I would like to keep track of the current time in the RTC. I read that TimeLib.h and RTClib.h don’t like each other in the same code, so are there any alternatives to the RTClib, which work with the TimeLib.h?

Now that you suggested this library, my only question is how I can convert an unsigned long variable to a normal datetime format in a way, which is “independent” from the RTC.

The code you posted does not set the date into the RTC as you said. You apparently also have not investigated any of the conversion functions in the Time library, which would accomplish your goal. Instead, you blindly run and post an example sketch that only demonstrates a few features of the library.

What you have done is independent of the RTC.

Look at the library RTC examples to see how to synchronize the time in your sketch with the RTC.

Yeah, my bad, I just wanted to know where to start looking for the solution. Fortunately, I made the code and it works like a charm. Thank you for helping!