I have a curious problem... I'm looking for a way to get tomorrows date.
Building a "simple" function that simply adds 1 to the day is more complicated than it seems.
It needs to know how many days are in a month, the amount of days in february. The 31st of December should increment the year etc...
It is manageable but there might be an easier way?
In JavaScript I could do:
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 1);
Convert the current time and date to unix time. Add 86400 seconds. Convert back. Use the date. Most time libraries support converting to and from unix time. If yours doesn't, you need a better one.
The algorithm for calculating ANY date are very well known. A Google search will teach you everything you need to know, including c/c++ code to implement the algorithm.
Alban:
I have a curious problem... I'm looking for a way to get tomorrows date.
Building a "simple" function that simply adds 1 to the day is more complicated than it seems.
It needs to know how many days are in a month, the amount of days in february. The 31st of December should increment the year etc...
It is manageable but there might be an easier way?
Occam's razor: things should be made as simple as possible, but no simpler...
You neglect to mention language, platform, library, anything about the context for writing this
function. Is this to do with RTC on an Arduino?
platform would be Arduino but I thought that would be obvious on the Arduino forum?
I have an array containing todays date:
byte today[] = { 15,10,14}; // 14th of October 2015
byte tomorrow[] = {0,0,0};
the array today[] will probably be populated using an RTC but for now it is hardcoded.
as I mentioned in my original post I ran into some example calculations which actually seem to work, didn't test dates like the 28th of February etc...
I now use but it might be more complex than needed:
byte today[] = { 15,10,14}; // 14th of October 2015
byte tomorrow[] = {0,0,0};
void setup() {
Serial.begin(9600);
CalcTomorrow(g(today[0],today[1],today[2]));
Serial.print(tomorrow[2]);Serial.print("-");Serial.print(tomorrow[1]);Serial.print("-");Serial.println(tomorrow[0]);
}
void loop() {
}
long g(int y, int m, int d) {
//http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
m = (m + 9) % 12;
y = y - m / 10;
return 365 * y + y / 4 - y / 100 + y / 400 + (m * 306 + 5) / 10 + (d - 1);
}
void CalcTomorrow(long g) {
//http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
g = g + 1; // add one day
int y = (10000 * g + 14780) / 3652425;
int ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
if (ddd < 0) {
y = y - 1;
ddd = g - (365 * y + y / 4 - y / 100 + y / 400);
}
int mi = (100 * ddd + 52) / 3060;
int mm = (mi + 2) % 12 + 1;
y = y + (mi + 2) / 12;
int dd = ddd - (mi * 306 + 5) / 10 + 1;
tomorrow[0] = y;
tomorrow[1] = mm;
tomorrow[2] = dd;
}
It feels like I answered my own question :-[
But feel free to add suggestion and comments.
Alban:
It feels like I answered my own question :-[
I thought reply #1 answered it very well. The other two also added great context. So I don't understand the reason for that statement. Your approach seems to be adhering to a do it yourself philosophy. It's great from a learning perspective, but it is a little like re-inventing the wheel. This problem space has been covered so many times.
that statement was only meant about the solution I found in between my first posting and the second.
If I had found this solution a bit sooner I wouldn't have posted the question in the first place so in a way I answered my own question.
The solution PaulS posted is actually easier and I'm very greatfull for his input and the input from the others.
I think my first problem was not thinking of arduino as C++ which made me google for adding days in arduino while adding days in C++ gave me the correct examples The other programming languages I often use all have functions that do that out of the box...