How to get 12hr time from a DS3231 RTC

After a lot of searching the web and thinking. I can up with a simple (and now obvious) solution to getting 12hr time from your DS3231 using the DS321.h library (Source: DS3231)

Note: I wrote this code for a tft display but it should work the same for serial of LCD display (I added Serial.begin(9600); and serial to all print lines for simplicity, the code below should work for serial).

Heres the basic code:

#include <DS3231.h>
DS3231  rtc(SDA, SCL);
// Init a Time-data structure
Time  t;

void setup(void) {
Serial.begin(9600);
rtc.begin();

}

void loop(void) { 
    // Get data from the DS3231
  t = rtc.getTime();
  int  Hour = t.hour;

  // Time Line

 serial.print("TIME: ");

  if (Hour <= 12){
      serial.print(rtc.getTimeStr());
      serial.print(" AM");}
  else{
      int hour12 = t.hour - 12; 
      if (hour12 < 10){
       serial.print("0");}
      serial.print(hour12);
      serial.print(':');
      if(t.min<10){
        serial.print("0");
      }
      serial.print(t.min, DEC);
      serial.print(':'); 
      if(t.sec<10){
        serial.print("0");
      }
      serial.print(t.sec, DEC);
      serial.print(" PM");
    }
}

The DS3231 can run in either 12h or 24h mode. In 12h mode, you don't have to do any conversion.

From the data sheet:

The DS3231 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the 12- or 24-hour mode select bit.

To avoid confusion with alarms and AM/PM, I run it in 24h mode and all system times are also in 24h mode.