How to combine time into a variable 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.println(finger.fingerID);
          Serial.print("Time-&gt;");
          Serial.print(now.hour(), DEC);
          Serial.print(':');
          Serial.print(now.minute(), DEC);
          Serial.print(':');
          Serial.print(now.second(), DEC);
      }


Sample result will be:

Found ID#1
Time-&gt; 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();
You want to use snprintf for this:

char timeString[9];

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


By using a format string like this you are taking into account time portions &lt; 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.