January 25, 2021
Date and Time in Python

Python – Date and Time

We would cover date and time data type in Python. Then we learn how to convert date and time data.


We would cover the following

  1. What is a Tick?
  2. Basics of Time Tuple
  3. Formatting Time Data
  4. Creating a Calendar
  5. The time Module
  6. The calendar Module



Let’s start by explaining the concept of tick

1. What is a Tick?

A tick is an interval of time in Python given in seconds. This is number of seconds from 12:00 a.m, January 1 1970.

Python contains a module called time. This modules provides what you need to work with time in Python. To use it, you need to add import time statement.

So to get the current time, you use the function time.time(). Let’s see how it work.


import time

currentTime = time.time()


The above code would give the output of 1549478219.6221974. This is because it returns time in seconds.


2. Basics of Time Tuple

You remember a tuple is a collection of items. You can review tuples here. Now, in Python, time is represented as a tuple of 9 items. These 9 items are explained in the table below

04-digit year2019
1Month number1 to 12
2Day of month1 to 31
3Hour0 to 23
4Minute0 to 59
5Second0 to 61 (60 or 61 are leap-seconds)
6Day of Week number0 to 6 (0 is Monday, 1 is Tuesday, etc)
7Day of year1 to 366 (Julian day)
8Daylight savings-1, 0, 1, -1 means library determines DST


3. Formatting Time Data

You can get formatted time by using the format by using the asctime() function. This would produce time in a friendly format.

For example

import time

currentTime = time.asctime()

If you run the code above, then you will have a formatted time as shown below:

Wed Feb  6 22:27:33 2019



4. Creating a Calendar

You can create and display a calendar in Python. You can do this using functions in the calendar module. Therefore, you need to import the calendar module.

For example, the code below would create a calendar.

import calendar

# Calender of February 2019
mycalendar = calendar.month(2019, 2)
print("Current Calendar")


If your run the code below, a calendar would be formatted and displayed as shown in below.

Current Calendar
   February 2019
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28



5. The time Module

The time module in Python provides many methods you can use to manipulate time. Especially for converting times between different formats.

Table below provides a list of methods available in the time module.

SNFunction and brief description
1time.altzoneGives the offset of the local DST timezone, in seconds west of UTC, if defined. It would be negative if the local DST timezone is east of UTC.You need to use this only if daylight is nonzero.
2time.asctime([tupletime])Returns a readable 24-character string such as ‘Wed Feb 06 14:07:14 2019’. Takes a time tuple as parameter
3time.clock( )Returns the current CPU time. Return value is a floating-point number of seconds. Based on performance,  the value of time.clock is more useful than that of time.time().
4time.ctime([secs])Similar to asctime(localtime(secs)) but without arguments is like asctime( )
5time.gmtime([secs])Returns a time-tuple t with the UTC time. Accepts an instant expressed in seconds since the epoch as parameter. Note : t.tm_isdst is always 0
6time.localtime([secs])Returns a time-tuple t with the local time (t.tm_isdst is 0 or 1, depending on if DST applies to instant secs by local rules). Takes an instant expressed in seconds since the epoch  as parameter
7time.mktime(tupletime)Returns a floating-point value with the instant expressed in seconds since the epoch. It takes an instant expressed as a time-tuple in local time and
8time.sleep(secs)Use this to  suspends the calling thread for the specified seconds.
9time.strftime(fmt[,tupletime])It returns a string representing the instant as given by string fmt. Accepts an instant expressed as a time-tuple in local time
10time.strptime(str,fmt=’%a %b %d %H:%M:%S %Y’) Returns the instant in time-tuple format of a given string. Parses the given str according to format string fmt
11time.time( )You used this to get the current time instant, a floating-point number of seconds since the epoch.
12time.tzset()Resets the time conversion rules that is used by the library routines. The environment variable TZ shows how this is done.


6. The calendar Module

Functions related to calendar are provided by the calendar module. For example,  function to display a  complete calendar for a particular month.

By default, Monday is taken as the first day of the week. Then Sunday is regarded as the last day. However, the default behavior can be changed. To change is, use the calender.setfirstweekday() function.

A list of functions available in the calendar module is given below:

SN.Function and brief description

Returns a multi-line string with a calendar for year year formatted into three columns separated by c spaces. w specifies the width in characters of each date; each line has length 21*w+18+2*c. l is the number of lines for each week.

2calendar.firstweekday( )

Returns the current setting for the weekday that starts each week. By default, when calendar is first imported and used, it is 0, which means Monday.


Returns True if the specified year is a leap year. Otherwise false


Gives the total number of leap days in the years within given  range(y1,y2).


Returns a multi-line string with a calendar for the specified month of the year. One line per week plus two header lines. w is the number of character (width) of each date; each line has length 7*w+6. l is the number of lines for each week.


Returns a list of lists of ints. Each sublist denotes a week. Days that are outside the given month month of the specified year are set to 0; days within the month are set to their day-of-month, 1 and up.


Returns two values: code of the weekday for the first day of the specified month in year year and  t number of days in the month. Weekday codes are by default 0 (Monday) to 6 (Sunday); month numbers are 1 to 12. 1 for January, 2 for February etc


Similar to print calendar.calendar(year,w,l,c).


Similar to print calendar.month(year,month,w,l).


Used to set the first day of each week to weekday code weekday. Weekday codes are 0 (Monday) to 6 (Sunday) by dafault


Gives the inverse of time.gmtime: takes a time instant as parameter in time-tuple form and returns the same instant as a floating-point number of seconds since the epoch.


Returns the weekday code for the given date.  By default, weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 (January) to 12 (December).

0 0 vote
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

[…] 13. Manipulating Date and Time Data in Python […]