Python Programming/Date and time

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Examples:

  • time.sleep(5)
    • Sleep for five seconds. Keywords: pause, wait.
  • current_time = time.localtime()
    • Gets the current date and time, as object of time.struct_time type.
  • print time.strftime("%Y-%m-%d", time.localtime())
    • Outputs the current date in the ISO format, e.g. 2016-03-20.
  • baseClock = time.clock(); time.sleep(5); print time.clock() - baseClock
    • Outputs the time elapsed in seconds, as a floating-point number. Is slightly inaccurate.
  • print (datetime.date(2016, 3, 20) - datetime.date(2016, 2, 20)).days
    • Outputs number of days elapsed between two dates.
    • The type of the difference is datetime.timedelta.

External links[edit]