Here is code I recently used to calculate persons age in Years, Months, and Days on Arduino:
#include "RTClib.h"
RTC_DS3231 rtc; // Using DS3231 Real Time Clock
DateTime now = rtc.now(); // Current Date and Time
DateTime dob = DateTime(1984,8,16,0,0,0); // Date of Birth (1984.08.16 00:00:00)
int t0 = dob.year() * 12 + dob.month() - 1;
int t = now.year() * 12 + now.month() - 1;
int dm = t - t0;
int Y;
int M;
int D;
//Calculate age in Y/M/D
if(now.day() >= dob.day()){
Y = floor(dm/12);
M = dm % 12;
D = now.day() - dob.day();
}
else {
dm--;
t--;
Y = floor(dm/12);
M = dm % 12;
DateTime tmp = DateTime(floor(t/12),(t%12)+1,dob.day(),0,0,0);
D = (now.unixtime() - tmp.unixtime())/60/60/24;
}
Credit: I found this algorithm on stackoverflow.com and modified to work on Arduino.
April 2017
Leave a Reply