Date and time in Python

Coding with Python

🕑 This lesson will take about 20 minutes

The datetime module

The datetime module is a module in Python that contains a collection of functions you can use that allow you to work with dates and time. Check out the example code below which shows how to get the current date and/or time, create dates, and format dates using functions from the datetime module.

The time module

Python has a useful time module that provides a range of functions for working with times, and converting different representations of time. Time intervals are floating-point numbers in units of seconds. In Python, a time is expressed as the number of seconds since 12:00am, January 1, 1970 (epoch). The function time.time() will return the current system time as the number of seconds since 12:00am, January 1, 1970. This is known as the Unix epoch. Let's have a look at an example.

The time module in Python provides a number of functions for working with time and converting time in to different formats, including:

  • time.altzone - provides the offset of the local DST (daylight saving time) timezone, in seconds west of UTC, if defined. The result will be negative if the local DST timezone is east of UTC. This is for use if daylight is nonzero.

  • time.asctime() - this function accepts a time-tuple and will return a readable 24-character long string eg. Sun Jul 9 072812 2017

  • time.clock() - this will return the current CPU time as a floating-point number of seconds value. This can be more useful than time.time() for measuring computational costs of different algorithms.

  • time.ctime([secs]) - this is like asctime(localtime(secs)), and without arguments is like asctime()

  • time.gmtime([secs]) - this acepts an instant of time expressed in seconds since Unix epoch, and will return a time-tuple t with the UTC time.

  • time.localtime([secs]) - this accepts an instant of time expressed in seconds since the Unix epoch and will return a time-tuple t with the local time.

  • time.mktime(tupletime) - this accepts an instant of time expressed as a time-tuple in local time, and will return a floating-point value with the instant of time expressed in seconds since the Unix epoch.

  • time.sleep(secs) - this will suspend the calling thread for secs seconds.

  • time.strftime(fmt[,tupletime]) - this will accept an instant of time expressed as a time-tuple in local time, and will return a string value that represents the instant of time as specified by fmt.

  • time.time() - this will return the current instant of time as a floating-point value in number of seconds since the Unix epoch.

Example of time.asctime():

The calendar module

The calendar module provides a range of calendar-related functions such as printing a text calendar for a given month or year. The calendar takes Monday as the first day of the week by default but this can be changed by calling the calendar.setfirstweekday() function. Before using any calendar functions, make sure you import the calendar module.

Here are just some of the functions in the calendar module:

  • calendar.isleap(year) - this will return True if year is a leap year, otherwise it will return False.

  • calendar.leapdays(year1, year2) - this will return the number of leap years in the years within the range of year1 and year2.

Example of calendar.isleap(year) :