 # Converting time from 24 hour to 12 hour

Hi, so i have this code which displays the time on a rgb lcd screen in 24 hour clock format. Is there a way to change this to 12 hour and have the lcd display am and pm beside the time ? I tried using the command hourFormat12 but i didn’t do anything… HELP

``````#include <Wire.h>
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(7,8,9,10,11,12);

int backLight = 13; // pin 13 will control the backlight

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.write(0);
Wire.endTransmission();
// A few of these need masks because certain bits are control bits
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
}
void setup()
{
lcd.setCursor(0,0);
lcd.begin(16,2);
lcd.print("The Clock");
lcd.setCursor(0,1);
delay(5000);
lcd.clear();
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);
// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 0;
minute = 24;
hour = 8;
dayOfWeek = 1;
dayOfMonth = 4;
month = 11;
year = 13;

//setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
lcd.begin(16, 2); // tells Arduino the LCD dimensions

}
void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.setCursor(0,1);
lcd.print(" ");
switch(dayOfWeek){
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}
lcd.print(" ");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(dayOfMonth, DEC);
lcd.print("/20");
lcd.print(year, DEC);
delay(1000);
}
``````

Is there a way to change this to 12 hour and have the lcd display am and pm beside the time ?

Simple arithmetic?

in what part of the code ?

in what part of the code ?

After this part`getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);`

so im probably guessing that you have to work with the part that says lcd.print(hour, DEC); but what do you have to change the code to to display the time in 12 hour format, im really confused....

I don't understand how you're confused. I assume you know what 12-hour and 24-hour time formats are. Ignoring Arduinos and computers for the moment, given a time in 24-hour format, how would [u]you[/u] convert it to 12-hour format?

if this part

&hour,

comes back as >12, subtract 12 and set a AM/PM flag to indicate PM.

How does "Modulo" help? A simple test of the hours being returned is all that is needed.

CrossRoads: How does "Modulo" help? A simple test of the hours being returned is all that is needed.

hour=hour%12; perhaps? Just a thought.

hour=hour%12; perhaps?

And at noon?

Real programmers have zero based clocks ;)

Which die in 2038/

AnyMajorDude:
Which die in 2038/

Only if they are using a signed epoch variable. The Time library uses an unsigned long value, so it’s good until well beyond year 2100. Run this sketch if you don’t believe me.

``````/*
* Test Y2038 rollover
* by aarg on the Arduino forum
*/
#include <TimeLib.h>

void setup()  {
Serial.begin(9600);
setTime(3, 14, 5, 19, 1, 2038);
}

void loop() {
while (millis() < 5000) {
digitalClockDisplay();
Serial.println((signed long)now());
delay(1000);
}
}

void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}

void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
``````