DS1307 Display Issue

Am using my RTC and noticed that when the hour displays a single digit my display shows 2 m’s. How do I add the “0” in front of a single hour digit?Here’s my coding…

  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.setCursor(0, 1);
  if(hour>0)
  {
    if(hour<=12)
    {
      lcd.print(hour, DEC);
    }
    else
    {
      lcd.print(hour-12, DEC);
    }
  }
  else
  {
    lcd.print("12");
  }
  lcd.print(":");
  if (minute < 10) {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  //lcd.print(":");
  //if (second < 10) {
  //  lcd.print("0");
  //}
  //lcd.print(second, DEC);
  if(hour<12)
  {
    lcd.print("am");
  }
  else
  {
    lcd.print("pm");
  }
  //lcd.print(" ");
  delay(1000);

Try this.

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
char buffer[12];
sprintf(buffer, "%02d:%02d:%02d", hour, minute, second);
lcd.setCursor(0, 1);
lcd.print(buffer);

or with AM/PM notation

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
char c = 'A';
if (hour > 12) c = 'P';
char buffer[12];
sprintf(buffer, "%02d:%02d:%02d %cM", hour%12, minute, second, c);
lcd.setCursor(0, 1);
lcd.print(buffer);

How do I add the "0" in front of a single hour digit?Here's my coding...

The same way you did it with the minutes?

liquidarts: Am using my RTC and noticed that when the hour displays a single digit my display shows 2 m's. How do I add the "0" in front of a single hour digit?Here's my coding....

Add a single trailing space in case the hours are in the range 0 to 9?