Time and date

Python can get the system time using the module time. To use this module type: import time.

The first line returns all variables required (year,month,day,hour,minute). The function time.time() returns ticks, which are system ticks every computer holds. The function localtime() converts these ticks into the actual human readable values.

timenow = time.localtime(time.time())

Type the program shown below and run it:

import time

timenow = time.localtime(time.time())
year,month,day,hour,minute = timenow[0:5]

print(str(day) + "/" + str(month) + "/" + str(year))
print(str(hour) + ":" + str(minute))

Study drill

Try the exercises below

1. Print the date in format year-month-day

After completing these continue with the next exercise.

Scope
Class