time

What it is good for?

Simple handling of times and dates.

The functions in time return the time and date in a structured format that can be formated to custom strings.

Installed with Python by default

yes

Example

The time module offers functions for getting the current time and date.

  1. import time
  2. print(time.asctime())
  3. print(time.strftime('%a %d.%m.', time.localtime()))

Wait for two seconds:

  1. time.sleep(2)

The datetime module also helps to format dates:

  1. date = datetime.date(2015, 12, 24)
  2. date.strftime("%d.%m.%Y")

Dates can be converted to integer numbers:

  1. date = datetime.date(2015, 12, 24)
  2. number = date.toordinal()

and back

  1. datetime.date.fromordinal(7)

Where to learn more?