 # DS3234 Date calculations

Hello,

I need some help with programming for the DS3234. Specifically Date calculations.

I'm using Spark Fun's DeadOn RTC DS3234 and an Uno.

I've played with JeeLabs RTClib for the DS3234, and can easily set the datetime and output the datetime via the serialmonitor.

What I want to achieve is to simply work out the number of days between the current date and a date that I have set. All I need to output is the number of elapsed days as a number. I just have no idea how to achieve it.

Any help would be greatly appreciated.

Convert both your dates to Julian Date and subtract one from the other to get the difference...

use integer variables

A = Y/100; B = A/4; C = 2-A+B; E = 365.25x(Y+4716); F = 30.6001x(M+1); JD= C+D+E+F-1524.5;

use integer variables

use long (especially for JD) as julian date's can exceed the integer limits.

added some naming to the code above to better understand it.

Definition: The Julian date (JD) is a continuous count of days from 1 January 4713 BC.

``````long JulianDate(int year, int month, int day)
{
long centuries = year/100;
long leaps = centuries/4;
long leapDays = 2 - centuries + leaps;         // note is negative!!
long yearDays = 365.25 * (year + 4716);     // days until 1 jan this year
long monthDays = 30.6001* (month + 1);    // days until 1st month
long jd = leapDays + days + monthDays + yearDays -1524.5;
return jd;
}
``````

There are also formulas for calculating back to Gregorian e.g. when will I be 10000 or 20000 days old. - http://aa.usno.navy.mil/faq/docs/JD_Formula.php - - http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html -

coding this is left as an exercise for the reader :)

Thanks guys. Much appreciated.

Now to give it a go! :)

Ran into similar problem recently, just wanted to share my solution. The idea behind my solution is to take a date, far back in time, I’ve taken 1/1/2015 (D/M/Y format) ( actually “any” other reasonable date can be used with my function ) and simply calculate how many days passed since that date using a simple function:

``````//==========================================================
//CODE CALCULATES DAYS BETWEEN NOW AND 1ST JAN 2015
int daysSince2015 (int days, int months, int years)
{
int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};

int daysPassed = 0;

int startDays = 1;
int startMonth = 1;
int startYears = 2015;

//KEEP SCROLLIN' TILL THE REQUIRED YEAR IS REACHED
for (startYears = 2015; startYears != years; startYears ++)
{
for (int n = 0; n < 12; n++)
{
//ONE MONTH AT A TIME
daysPassed += daysPerMonth [n];
if ((n == 1) && ((startYears % 4) == 0))
{
daysPassed += 1;
}
}
}

//SCROLL MONTHS IF THE MONTHS IS NOT JANUARRY
if (startMonth != months)
{
//SCROLL MONTH, USE startMonth AS A CURSOR TO daysPerMonth ARRAY
//KEEP IN MIND THAT 1ST ELEMENT OF daysPerMonth ARRAY IS AT daysPerMonth
for (startMonth = 1; startMonth != months; startMonth ++)
{
//ONE MONTH AT A TIME
daysPassed += daysPerMonth [startMonth - 1];
//ADD A DAY IF IT'S FEBRUARY OF A LEAP YEAR
if ((startMonth == 2) && ((startYears % 4) == 0))
{
daysPassed += 1;
}
}
}

//SINCE WE ARE ON THE SAME MONTH AND YEAR, JUST CALCULATE DAY DIFFERENCE
daysPassed += days - startDays;

//PRINT THE RESULT
Serial.print("DAYS PASSEDS: ");
Serial.println(daysPassed);

//RETURN THE RESULT
return daysPassed;

}
//=============================================================
``````

Calculate this function for both dates of interest, the result of subtraction will represent the days difference between dates. The function was tested, and the results are proven using : Calculate Duration Between Two Dates – Results

``````  for (startYears = 2015; startYears != years; startYears ++)
``````

That's going to produce some strange results for January 18, 2014.

Two quite obvious ways you can avoid it:

• Either use with dates that come after 01.01.2015
• Or , perhaps replace 01.01.2015 with something else, you even can use Epoch date. As far as you use 1st day of the 1st month it will work.

I use the given code to work with meaningful dates (present and future), as I cant see how would I need dates from past in my automation projects. I wrote this function as I had to calculate how many days in between of a current date and a date in the future, for me this approach works.

How I normally use this function:

int startDays = daysSince2015(guardDay, guardMonth, guardYear); int endDays = daysSince2015(Td, TM, Ty); int daysInbetween = endDays - startDays;

And, sure, start date is earlier than the end date.

robtillaart: There are also formulas for calculating back to Gregorian e.g. when will I be 10000 or 20000 days old. - http://aa.usno.navy.mil/faq/docs/JD_Formula.php

Reference: Fliegel, H. F. & van Flandern, T. C. 1968, Communications of the ACM, 11, 657.

Code reference to the Apollo 11 ACM (Apollo Command Module)

Hehe, anybody remembers? Apollo11 was a big success, landing the first man on the moon, so perhaps the date algorithm will work successfully, too. :grinning:

Two quite obvious ways you can avoid it:

Either use with dates that come after 01.01.2015 Or , perhaps replace 01.01.2015 with something else, you even can use Epoch date. As far as you use 1st day of the 1st month it will work.

Whichever date is used, the program should properly handle out-of-range dates. That was my point. For some definition of "properly", of course.

The Command Module might have been OK, but the Lander had a "bug" (actually operator error) that almost caused the mission to be scrubbed.

Use the Time library's makeTime function. When you have separate year, month, etc from the RTC, store those values in a tm_elements structure (I think I've got its name right). Then use makeTime to convert that to number of seconds since the beginning of 1970. Do that for both date/times. Then subtract.

Pete P.S. I didn't notice how dead the original thread was until after I'd posted this.