Date + days = newdate

Does some one has a (simple) code to add a certain amount of days to a certain date ?

e.g. 10/08/17 + 100days = xx/xx/xx.

maybe there's a library that can do this ?

thanks in advance.

PS. I use the RTC3231

The key to working with time is to have a known "base" time to calculate everything relative to. This is called the epoch. Different systems use different epochs, and each has advantages and disadvantages.

I generally use the UNIX epoch in my programming, which is the first of January, 1970 (01/01/1970). This is year, month, day, hour, minute and second 0.

From that you can then calculate the number of seconds between the epoch and any other time you choose as long as it is later than the epoch.

To calculate the day of the week for any given date you first need to know the day of the week for the epoch (for UNIX that's a Thursday) then work out how many days there are between the epoch and the target time.

Calculating real times in terms of seconds can be somewhat tricky due to leap years and daylight savings, but there are plenty of algorithms in the public domain, and many RTC libraries include functions to convert to/from epoch times.

For instance, the Arduino Time Library has functions to calculate days, and to convert to "time_t" format (seconds since epoch) which can be used to work out differences in times. For instance, if you know that you want 100 days in the future - so you can get the current time in seconds from the epoch then add to that 100 days' worth of seconds (100 × 60 × 60 × 24) to that time to get the same time of 100 days later.

WOW billhowl !!! That was fast !!! Tanx, tanx, tanx !

your explanation is incredibly usefull !
the links you provided are SUPER !
gonna start programming right-away !

some calculations will be necessary, but good that our Arduino is great in that !

thanks again ! :slight_smile:

Hopefully I don't encounter problems. If I do, can I can come back here ? :wink:

hmmm... :slight_smile:
All very interesting lecture, I've read through it a couple of times ... but I can't find anywhere HOW to calculate the epoch (time_t) for a certain date (NOT now() !) ...

I have a

byte Day;       // e.g. 28
byte Month;     // e.g. 8
int Year;       // e.g. 17

how do I calculate "time_t" for this date ?

please help me on my way ... (tanx in advance ! :wink: )

I think it's called makeTime; not behind a pc at the moment.

Here is the code for maketime

// include header file for time function
#include <Time.h>

byte Day=28;      // e.g. 28
byte Month=8;     // e.g. 8
int Year=2017;    // e.g. 2017

tmElements_t tm;

void setup() {
	Serial.begin(9600);
}

void loop()  {
	time_t nextMakeTime;

	tm.Second = 0; 
	tm.Hour = 0;
	tm.Minute = 0;
	tm.Day = Day;
	tm.Month = Month;
	tm.Year = Year - 1970;   // offset from 1970;
	nextMakeTime =  makeTime(tm); // convert time elements into time_t

	Serial.println(nextMakeTime);   
	Serial.println(day(nextMakeTime));   
	Serial.println(month(nextMakeTime));   
	Serial.println(year(nextMakeTime));   
	delay(3000);
}

WOW billhowl ! That is really fantastic ! You wrote the whole code for ME !! 1K x Thanks!
I've been fiddling around with your code, and it's really simple and understandable. Too bad that good (full) explanations about the libraries and their functions are hard to find. Nevertheless... I encoutered a weird (to me!) problem. When I Serial.print the results of your code, everything works just fine. But when I print the results to an LCD display, it adds two characters behind the month and the year. (two characters that I made myself.)

// "arrow up"                  [^]
byte customChar2[] = 
{
  B00100,
  B01110,
  B11111,
  B00100,
  B00100,
  B00100,
  B00100,
  B00000
};
// plus/minus                [+/-]
byte customChar5[] = 
{
  B01000,
  B11100,
  B01001,
  B00010,
  B00100,
  B01000,
  B10000,
  B00111
};

void setup ()
{
  lcd.createChar(2, customChar2);
  lcd.createChar(5, customChar5);
}

I have this code for the calculation and display :

    time_t nextMakeTime;
    tm.Day = startDay;
    tm.Month = startMonth;
    tm.Year = startYear + 2000 - 1970; // offset from 1970;
    nextMakeTime =  makeTime(tm) + (period1 + period2) * 86400 ;


    lcd.setCursor (8, 3);
    lcd.print(day(nextMakeTime));
    lcd.print("/");
    lcd.println(month(nextMakeTime));
    lcd.print("/");
    lcd.println(year(nextMakeTime) - 2000);

    //Serial.println(nextMakeTime);
    Serial.print(day(nextMakeTime));
    Serial.print("/");
    Serial.print(month(nextMakeTime));
    Serial.print("/");
    Serial.println(year(nextMakeTime) - 2000);
    Serial.println();

The Serial.print shows : (e.g) 28/6/18 ---> PERFECT !!
The lcd.print shows then 28/6+/-^/18+/-^ ---> ??? (+/- is ONE character)

Any idea what's happening here ?

Again, thanks for your help !!!

Try using print instead of println.

HAHA ! Thanks, Wildbill ... just figured it out myself ! :grin:
Nevertheless : thanks and congratz with your SFFF (Super-Fast-Fault-Finding)

it works just fine now !

Thanks to all contributers here, I hope one day you'll all become rich and famous :slight_smile:
I also hope that other Arduino-freaks who struggle with dates will find their way here !

10K x thanks !!!