user51275: 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->");
          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-> 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();


user4143: 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 < 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.

51275: <p>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:</p>

<pre><code>    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);
      }
</code></pre>

<p>Sample result will be:</p>

<pre><code>Found ID#1
Time-&gt; 18:11:54
</code></pre>

<p>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???</p>

<p>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?</p>

<pre><code>string time = now.hour() + ':' + now.minute() + ':' + now.second();
</code></pre>
 4143: <p>You want to use <code>snprintf</code> for this:</p>

<pre><code>char timeString[9];

snprintf(timeString, 9, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
Serial.println(timeString);
</code></pre>

<p>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.</p>
