Format Date and Time using sprintf?

dc42:
I agree, it's better to print the parts of the date independently, rather than use precious RAM up by declaring a buffer for sprintf to write to. RAM typically runs out before program memory on the atmega328. The only thing I would do differently from your example is to write single characters using Serial.write, e.g. Serial.write('/') instead of Serial.print("/"), and when writing string that are more than 1 character long, use flash strings, e.g. Serial.print(F("a string")).

If you really want to use sprintf, use something like:

sprintf(buffer, "%02d/%02d %02d:%02d", Day, Month, Hour, Minute);
Serial.print(buffer);

where buffer is a char array that is at least 13 characters long.

Thanks for that, I need to check with some more demo code that the Serial.print(F("string") works when the the time is updated, but using serial.write for single chars is a good idea.

Eventually the Date and time will be stored as arrays of strings (or chars) for later retrieval using PROGMEM.
I need to look into the best way of doing that (more demo code!) :slight_smile: