Python Programming/Date and time

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.
  • print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    • Outputs the current date and time, e.g. 2020-04-09 13:37:28.
  • 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