Go Down

Topic: DS3234 Date calculations (Read 1 time) previous topic - next topic

bjaaz

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.

abrookfield

#1
Mar 04, 2012, 05:46 am Last Edit: Mar 04, 2012, 06:16 am by abrookfield Reason: 1
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;

www.reeftopper.com

robtillaart

Quote
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.

Code: [Select]
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 :)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bjaaz

Thanks guys. Much appreciated.

Now to give it a go! :)

Go Up