Printing leading zeroes

 Serial.print(" current time ");
    Serial.print(current.year(), DEC);
    Serial.print('/');
    Serial.print(current.month(), DEC);
    Serial.print('/');
    Serial.print(current.day(), DEC);
    Serial.print(' ');
    Serial.print(current.hour(), DEC);
    Serial.print(':');
    Serial.print(current.minute(), DEC);
    Serial.print(':');
    Serial.print(current.second(), DEC);
    Serial.println();

This gives output as current time 2013/3/27 3:6:39

I want this to be " current time 2013/3/27 03:06:39"

How to implement the %2.2d format..?

Moderator edit: Thread split, topic title changed.

Print a leading zero if your number is less than 10.

Hijacked thread split.

Or, use sprintf() to format the data in a string, and then print the string. Use %02d to get a leading 0 when the value is less than 10.

PaulS: Or, use sprintf() to format the data in a string, and then print the string. Use %02d to get a leading 0 when the value is less than 10.

sprintf Ref: http://www.cplusplus.com/reference/cstdio/sprintf/ snprintf Ref: http://www.cplusplus.com/reference/cstdio/snprintf/

Should I have to create separate strings for DD:MM:YY & HH:MM:SS…? 6 separate strings.?

nightcrawler218:
Should I have to create separate strings for DD:MM:YY & HH:MM:SS…? 6 separate strings.?

No, just do this:

char buf[50];
sprintf(buf, "%2d:%2d:%2d %2d:%2d:%2d", days, months, years % 10, hours, minutes, seconds);
Serial.println(buf);
28: 3: 3  7:17:59
28: 3: 3  7:18: 0
28: 3: 3  7:18: 1
28: 3: 3  7:18: 2
28: 3: 3  7:18: 3
28: 3: 3  7:18: 4
28: 3: 3  7:18: 5
28: 3: 3  7:18: 6
28: 3: 3  7:18: 7
28: 3: 3  7:18: 8
28: 3: 3  7:18: 9
28: 3: 3  7:18:10
28: 3: 3  7:18:11

This is the output of the code…

// Date and time functions using just software, based on millis() & timer

#include <Wire.h>
#include "RTClib.h"

RTC_Millis RTC;
void setup () {
    Serial.begin(57600);
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.begin(DateTime(__DATE__, __TIME__));
}

void loop () {
    DateTime now = RTC.now();
    char buf[50];
    sprintf(buf, "%2d:%2d:%2d %2d:%2d:%2d", now.day(), now.month(), now.year() % 10, now.hour(), now.minute(), now.second());
    Serial.println(buf);
    delay(1000);
}

For leading 0's you have to have a 0 in there. Did you read the link I gave you? %02d

WizenedEE:
For leading 0’s you have to have a 0 in there.
Did you read the link I gave you?
%02d

Haven’t found your link in this post sir. Kindly share the link.

nightcrawler218:

WizenedEE: For leading 0's you have to have a 0 in there. Did you read the link I gave you? %02d

Haven't found your link in this post sir. Kindly share the link.

Sorry I was thinking of another thread. http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1