Monday, 6 February 2023

Feb05 Display time and date using f-string

#Libraries from machine import Pin, I2C #,RTC , WDT from ssd1306 import SSD1306_I2C import utime #set up WIDTH =128 HEIGHT= 64 i2c=I2C(0,scl=Pin(17),sda=Pin(16),freq=200000) oled = SSD1306_I2C(WIDTH,HEIGHT,i2c) week=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"] while True: #time.sleep(1) date_time_now = utime.time() date_time_now=utime.localtime(date_time_now) print(date_time_now) #date_time_now is in this tuple format #(2022, 12, 15, 14, 54, 27, 3, 349) # 0 1 2 3 4 5 6 7 #local_date_time=str(date_time_now[3]) + ':' + str(date_time_now[4]) + ':' + str(date_time_now[5]) local_year_day=f"{date_time_now[0]}/{date_time_now[1]:02}/{date_time_now[2]:02}" print(local_year_day) local_date_time=f"{date_time_now[3]:02}:{date_time_now[4]:02}:{date_time_now[5]:02}" print(local_date_time) local_wk_day=f"{week[date_time_now[6]]}" print(local_wk_day) #display in ssd1306 oled.fill(0) #clear display oled.text("Today's date is ",0,0) oled.text(local_year_day, 0, 14) oled.text(local_wk_day,0,28) oled.text("Current time is ", 0,42) oled.text(local_date_time, 0, 56) oled.show() utime.sleep(5)

No comments: