I have an Arduino Uno project which is recording student attendance using fingerprint sensor. So when sensor found a match, the serial monitor will be printing out Student ID and current time like this:

    DateTime now;
    void setup()

    void loop()
          //found a match
          Serial.print("Found ID #");
          Serial.print(now.hour(), DEC);
          Serial.print(now.minute(), DEC);
          Serial.print(now.second(), DEC);

Sample result will be:

Found ID#1
Time-> 18:11:54

I would like to store the time into a variable and print it out instead of printing hour, minute, second separately. How can I do that???

I have tried to do like this as below but im getting error, I would like to store it as a string. How can I do that?

string time = now.hour() + ':' + now.minute() + ':' + now.second();
  • It's going to be more efficient to print piece by piece. Because it constructs the text straight into the output buffer instead of needing to copy it over. – ratchet freak Nov 16 '18 at 10:41
  • 1
    Hint: If you want to do some calculations or store a timestamp the data type time_t is a better choice. It is defined as seconds since midnight Jan 1 1970 (aka unix time). For more details see github.com/PaulStoffregen/Time/blob/master/TimeLib.h#L21 – Mikael Patel Nov 16 '18 at 11:52

You want to use snprintf for this:

char timeString[9];

snprintf(timeString, 9, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());

By using a format string like this you are taking into account time portions < 10 (like 7 minutes and 23 seconds past 3 am, which would otherwise be 3:7:23) by padding the time with leading zeroes where needed.

  • Damn, you were three minutes quicker with the same solution. Replacing 9 with sizeof timeString could be useful in case he ever wants to include more data into his time stamp. – towe Nov 16 '18 at 10:45
  • Could be - as long as timeString is in the same (or greater) scope and not a pointer passed to a function :) – Majenko Nov 16 '18 at 10:46
  • @Majenko Do you familiar with python or firebase? Cause I would like to send the output to the python code, for the python code itself will catch the output and push it to firebase real time database. I refer to this website , I made the output like this Serial.println("{'SID':"+ String(finger.fingerID) +",'time':"+ String(timeString) +"}"); but I don't know how to catch it in my python – Michael Nov 16 '18 at 15:47
  • I loathe python with a passion, and I have never heard of firebase. – Majenko Nov 16 '18 at 16:08
  • @Majenko It's ok. I do the same thing to date. But I can display date like this2018/11/. Day cannot display, what happened? – Michael Nov 18 '18 at 9:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.