how to format 12 hours time in Raspi lcd with I2C

enter image description here
I would like to change my lcd 24 hours time format to 12 hours

import lcddriver
import time
import datetime

display = lcddriver.lcd()

try:
    print("Writing to display")
    display.lcd_display_string("Time", 1) 
    while True:
        display.lcd_display_string(str(datetime.datetime.now().time()), 2)            

except KeyboardInterrupt:
    print("Cleaning up!")
    display.lcd_clear()

Upvotes: 0

Views: 230

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71689

You could use datetime module in python like this:

import lcddriver
import time
import datetime

display = lcddriver.lcd()

try:
    print("Writing to display")
    display.lcd_display_string("Time", 1) 
    while True:
        datestr = datetime.datetime.now().strftime("%I:%M:%S %p")
        display.lcd_display_string(datestr, 2)            

except KeyboardInterrupt:
    print("Cleaning up!")
    display.lcd_clear()

For example, if current time is 15:40:50 then datetime.datetime.now().strftime("%I:%M:%S %p") outputs 03:40:50 PM

Hope it helps you!

Upvotes: 1

Related Questions