Monday, 8 February 2016

Raspberry Pi - All things Date and Time in Python

When writing python code you often want to compare times dates and add delays etc. Here are a few things I have learnt that may help you in your projects.

Time delays

These are easy to use and are very useful all you need is to define time then use the sleep function.


print "Waiting 10 seconds"
print "Running again"

The sleep function can also do delays less than a second for example 
time.sleep(0.01) this is sometimes used for some GPIO that may need a moment to complete in another thread.

Formatting Date time

You can convert datetime to various parts that you want to display

Here I create a date time to format:-
import datetime
MyDateTime = datetime.datetime(2016,1,3,8,30,20)

Simple date and time extractions
print "Day of month :",
print "Month :", MyDateTime.month
print "year  :", MyDateTime.year
print "Weekday Monday to Sunday (0 to 6) :", MyDateTime.weekday()
print "Weekday Monday to Sunday (1 to 7) :", MyDateTime.isoweekday()
Program output:-

Formatted date outputs
print "ISO Formatted date    :", MyDateTime.isoformat()
print "Long format date time :", MyDateTime.ctime()
Program output:-

String formatted dates 

print MyDateTime.strftime("Time       : %H:%M:%S")

print MyDateTime.strftime("Short Date : %d/%m/%y")

print MyDateTime.strftime("Long Date  : %d/%m/%Y")

print MyDateTime.strftime("D/Month/Y  : %d-%b-%y")
print MyDateTime.strftime("Full Date  : %A %d %B %Y")
Program output:-

For the complete reference of strftime click this link

How to compare dates and times and just the time

To compare first you need to input a date, time or both. To input a full date and time use the datetime function with year,day,month,hour,minute,second. Use the if statement with greater than (>) or less than (<) operators (never use == as the time is measured in microseconds so its unlikely match exactly).
import datetime
MyDateTime = datetime.datetime(2016,1,3,8,30,20)
print "Entered Date time is ", MyDateTime

if MyDateTime >
print "Current datetime is bigger"
print "My datetime is bigger"

To compare time only use the time function and compare the time only. can be used to do some operation the same time every day.

MyTimeOnly = datetime.time(8,31,53)
print "Entered time is ", MyTimeOnly

if MyTimeOnly > datetime.time():
print "Current time is bigger"
print "My time is bigger"
Program output:-

What is UTC time and what does it mean?

UTC means Coordinated Universal Time (This used to be known as Greenwich Mean Time, or GMT). UTC is the common time standard across the world.

So why UTC and not CUT?
The Official Abbreviation

The official abbreviation for Coordinated Universal Time is UTC. It came about as a compromise between English and French speakers.
  • Coordinated Universal Time in English would normally be abbreviated CUT.
  • Temps Universel CoordonnĂ© in French would normally be abbreviated TUC.
How to display the current time and UTC time
import datetime
print "The current local date time is ",

print "The current UTC date time is   ",datetime.datetime.utcnow()
Program output:-

The resolution of time is to the microsecond. The date is the February 2nd 2016 (My time zone is Sydney Australia so UTC -11 hours on that date).

To get the current time zone UTC offset do the following
import time
print "The UTC offset is",time.altzone/3600,"Hours"
Program output:-

The function time.altzone returns seconds so divide by 3600 to get hours offset


  1. Awesome! Very helpful. Thank you

  2. Thanks so much for this! Well written, great job!

  3. Hello and thanks for this - very useful.

    I am a bit stuck still though. I have input a time with MyTimeOnly = datetime.tme(8,20,0)
    Then I want to compare it to the current time - it is 11 am with MyTimeOnly < datetime.time() and it returns False while it should return True...
    If I try to print the current time with print(datetime.time()) it returns 00:00:00...

    Any lead to try to solve this is welcomed!

  4. Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog. python Online course


Thank you fro reading my page.
Let me know if there is anything that I could add or change.
Please let me know if this information is helpful.