Python DateTime Tutorial with examples

To work with Dates in Python, import the module datetime. Let us see what the datetime module is and how we can use it to manipulate dates and times. Additionally, we will work with the calendar modules and fully understand the concept of Python DateTime.

Therefore, we will work on two modules in this lesson:

  • datetime
  • calendar

Python datatime module

The datetime module has classes for date and time manipulation, such as getting the current date, fetching the month from a date, how to fetch week, month number, etc.

The classes available in the datetime module are:

Python datetime class

Get the current date and time

To get the current date and time in Python, import the datetime module:

import datetime

After that, use the now() built-in function:

datetime.now()

Now, let us see an example and get the current date and time:

# Get the current date and time in Python

import datetime as dt

print("Current date and time = ",dt.datetime.now())

The output is as follows:

Current date and time =  2026-03-09 22:31:35.538731

Get Today’s Date

To get today’s Date, first, import the date class from the datetime module:

from datetime import date

Let us now see an example to get today’s date in Python:

from datetime import date 

res = date.today()
print("Getting today's date:", res)

The output is as follows:

Getting today's date: 2020-05-30

Get Today’s Time with the datetime module

To get today’s time, first, import the date class from the datetime module:

# Get today's time with the datetime module

from datetime import datetime

res = datetime.now()

print(res.time())

Output

22:30:34.069947

Get the weekday (short) in Python

To get the weekday (short) like Mon, Tue, Wed, etc. in Python, use the %a format code. Let us see an example to get the weekday:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 4, 25) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%a"))

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%a"))

The output is as follows:

Date1 =  2020-05-30 18:57:19.938362
Date1 weekday =  Sat
Date2 =  2020-04-25 00:00:00
Date2 weekday =  Sat

Get the weekday (complete) in Python

To get the weekdays like Mon, Tue, Wed, etc. in Python, use the %A format code. Let us see an example to get the weekday:

#import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 7, 15) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))

The output is as follows:

Date1 =  2020-05-30 19:00:54.272196
Date1 weekday =  Saturday
Date2 =  2020-07-15 00:00:00
Date2 weekday =  Wednesday

Get the weekday as a number in Python

To get the weekday as a number, such as 0 for Sunday, 1 for Monday, 2 for Tuesday. 3 for Wednesday, etc., use the %w format code. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 6, 10) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))

The output is as follows:

Date1 =  2020-05-30 19:16:16.973650
Date1 weekday =  Saturday
Date1 weekday as number =  6
-------------------------
Date2 =  2020-06-10 00:00:00
Date2 weekday =  Wednesday
Date2 weekday as number =  3

Get the day of month in Python

To get the day of month, i.e. from 01 to 31, use the %d format code in Python. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 6, 10) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))

The output is as follows:

Date1 =  2020-05-30 19:25:24.602194
Date1 weekday =  Saturday
Date1 weekday as number =  6
Date1 day of month =  30
-------------------------
Date2 =  2020-06-10 00:00:00
Date2 weekday =  Wednesday
Date2 weekday as number =  3
Date2 day of month =  10

Get the month name (short) in Python

To get the month name in short, such as Jan, Feb, Mar, etc., use the %b format code. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 6, 10) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%b"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%b"))

The output is as follows:

Date1 =  2020-05-30 19:37:48.334488
Date1 weekday =  Saturday
Date1 weekday as number =  6
Date1 day of month =  30
Date1 month name =  May
-------------------------
Date2 =  2020-06-10 00:00:00
Date2 weekday =  Wednesday
Date2 weekday as number =  3
Date2 day of month =  10
Date2 month name =  Jun

Get the month name (complete) in Python

To get the month name, such as January, February, March, April, etc., use the %B format code. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020, 6, 10) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))

The output is as follows:

Date1 =  2020-05-30 19:44:40.504150
Date1 weekday =  Saturday
Date1 weekday as number =  6
Date1 day of month =  30
Date1 month name =  May
-------------------------
Date2 =  2020-06-10 00:00:00
Date2 weekday =  Wednesday
Date2 weekday as number =  3
Date2 day of month =  10
Date2 month name =  June

Display the month as a number in Python

To display the month as a number, such as 01 for January, 02 for February, 02 for March, etc., use the %m format code. Let us now see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2020,9, 27) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number  = ",date1.strftime("%m"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))

The output is as follows:

Date1 =  2020-05-30 19:59:22.731052
Date1 weekday =  Saturday
Date1 weekday as number =  6
Date1 day of month =  30
Date1 month name =  May
Date1 month number  =  05
-------------------------
Date2 =  2020-09-27 00:00:00
Date2 weekday =  Sunday
Date2 weekday as number =  0
Date2 day of month =  27
Date2 month name =  September
Date2 month number  =  09

Display year (short) in Python

To get the year (without century), such as 19 for 2019, 20 for 2020, etc., use the %y format code. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number  = ",date1.strftime("%m"))
print("Date1 year (without century)  = ",date1.strftime("%y"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))

The output is as follows:

Date1 =  2020-05-31 08:06:06.539699
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number  =  05
Date1 year (without century)  =  20
-------------------------
Date2 =  2019-08-22 00:00:00
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19

Display year (complete) in Python

To get the year (with century), such as 2018, 2019, 2020, etc., use the %Y format code. Now, let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number  = ",date1.strftime("%m"))
print("Date1 year (without century)  = ",date1.strftime("%y"))
print("Date1 year (with century)  = ",date1.strftime("%Y"))

print("-------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))

The output is as follows:

Date1 =  2020-05-31 08:18:14.047177
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number  =  05
Date1 year (without century)  =  20
Date1 year (with century)  =  2020
-------------------------
Date2 =  2019-08-22 00:00:00
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019

Display hour in 24-hour format with Python

To display the hour in 24-hour format (00 – 24), such as for 18 for 6PM, 22 for 10PM, 23 for 11PM, etc., use the %H format code. Let us now see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22, 15, 20, 50) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number = ",date1.strftime("%m"))
print("Date1 year (without century) = ",date1.strftime("%y"))
print("Date1 year (with century) = ",date1.strftime("%Y"))
print("Date1 hour (24-hour format) = ",date1.strftime("%H"))

print("----------------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))
print("Date2 hour (24-hour format) = ",date2.strftime("%H"))

The output is as follows:

Date1 =  2020-05-31 08:26:48.045070
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number =  05
Date1 year (without century) =  20
Date1 year (with century) =  2020
Date1 hour (24-hour format) =  08
----------------------------------
Date2 =  2019-08-22 15:20:50
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019
Date2 hour (24-hour format) =  15

Display hour in 12-hour format with Python

To display the hour in 12-hour format (00 – 12), such as for 6 for 6PM, 10 for 10PM, 11 for 11PM, etc., use the %I format code. However, the 24-hour format supports 0-24 format i.e. 18 for 6PM, 22 for 10PM, etc.

Let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22, 15, 20, 50) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number = ",date1.strftime("%m"))
print("Date1 year (without century) = ",date1.strftime("%y"))
print("Date1 year (with century) = ",date1.strftime("%Y"))
print("Date1 hour (24-hour format) = ",date1.strftime("%H"))
print("Date1 hour (12-hour format) = ",date1.strftime("%I"))

print("----------------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))
print("Date2 hour (24-hour format) = ",date2.strftime("%H"))
print("Date2 hour (12-hour format) = ",date2.strftime("%I"))

The output is as follows:

Date1 =  2020-05-31 08:32:32.757219
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number =  05
Date1 year (without century) =  20
Date1 year (with century) =  2020
Date1 hour (24-hour format) =  08
Date1 hour (12-hour format) =  08
----------------------------------
Date2 =  2019-08-22 15:20:50
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019
Date2 hour (24-hour format) =  15
Date2 hour (12-hour format) =  03

Display whether the date is AM/PM in Python

To display whether the date is AM/PM, use the %p format codes in Python. Let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22, 15, 20, 50) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number = ",date1.strftime("%m"))
print("Date1 year (without century) = ",date1.strftime("%y"))
print("Date1 year (with century) = ",date1.strftime("%Y"))
print("Date1 hour (24-hour format) = ",date1.strftime("%H"))
print("Date1 hour (12-hour format) = ",date1.strftime("%I"))
print("Date1 time in AM/PM = ",date1.strftime("%p"))

print("----------------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))
print("Date2 hour (24-hour format) = ",date2.strftime("%H"))
print("Date2 hour (12-hour format) = ",date2.strftime("%I"))
print("Date2 time in AM/PM = ",date2.strftime("%p"))

The output is as follows:

Date1 =  2020-05-31 08:45:04.158637
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number =  05
Date1 year (without century) =  20
Date1 year (with century) =  2020
Date1 hour (24-hour format) =  08
Date1 hour (12-hour format) =  08
Date1 time in AM/PM =  AM
----------------------------------
Date2 =  2019-08-22 15:20:50
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019
Date2 hour (24-hour format) =  15
Date2 hour (12-hour format) =  03
Date2 time in AM/PM =  PM

Display Day number (001-366) of year in Python

To display day number of year in Python, use the %j format code. Let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22, 15, 20, 50) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number = ",date1.strftime("%m"))
print("Date1 year (without century) = ",date1.strftime("%y"))
print("Date1 year (with century) = ",date1.strftime("%Y"))
print("Date1 hour (24-hour format) = ",date1.strftime("%H"))
print("Date1 hour (12-hour format) = ",date1.strftime("%I"))
print("Date1 time in AM/PM = ",date1.strftime("%p"))
print("Date1 day number of year = ",date1.strftime("%j"))


print("----------------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))
print("Date2 hour (24-hour format) = ",date2.strftime("%H"))
print("Date2 hour (12-hour format) = ",date2.strftime("%I"))
print("Date2 time in AM/PM = ",date2.strftime("%p"))
print("Date2 day number of year = ",date2.strftime("%j"))

The output is as follows:

Date1 =  2020-05-31 08:49:46.197748
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number =  05
Date1 year (without century) =  20
Date1 year (with century) =  2020
Date1 hour (24-hour format) =  08
Date1 hour (12-hour format) =  08
Date1 time in AM/PM =  AM
Date1 day number of year =  152
----------------------------------
Date2 =  2019-08-22 15:20:50
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019
Date2 hour (24-hour format) =  15
Date2 hour (12-hour format) =  03
Date2 time in AM/PM =  PM
Date2 day number of year =  2

Get the week number of year in Python

To get the week number of year in Python, use the %U format code. Let us see an example:

# import datetime module
import datetime

#date1 and date2
date1 = datetime.datetime.now()
date2 = datetime.datetime(2019, 8, 22, 15, 20, 50) 

print("Date1 = ",date1)
print("Date1 weekday = ",date1.strftime("%A"))
print("Date1 weekday as number = ",date1.strftime("%w"))
print("Date1 day of month = ",date1.strftime("%d"))
print("Date1 month name = ",date1.strftime("%B"))
print("Date1 month number = ",date1.strftime("%m"))
print("Date1 year (without century) = ",date1.strftime("%y"))
print("Date1 year (with century) = ",date1.strftime("%Y"))
print("Date1 hour (24-hour format) = ",date1.strftime("%H"))
print("Date1 hour (12-hour format) = ",date1.strftime("%I"))
print("Date1 time in AM/PM = ",date1.strftime("%p"))
print("Date1 day number of year = ",date1.strftime("%j"))
print("Date1 week number of year = ",date1.strftime("%U"))

print("----------------------------------")

print("Date2 = ",date2)
print("Date2 weekday = ",date2.strftime("%A"))
print("Date2 weekday as number = ",date2.strftime("%w"))
print("Date2 day of month = ",date2.strftime("%d"))
print("Date2 month name = ",date2.strftime("%B"))
print("Date2 month number  = ",date2.strftime("%m"))
print("Date2 year (without century)  = ",date2.strftime("%y"))
print("Date2 year (with century)  = ",date2.strftime("%Y"))
print("Date2 hour (24-hour format) = ",date2.strftime("%H"))
print("Date2 hour (12-hour format) = ",date2.strftime("%I"))
print("Date2 time in AM/PM = ",date2.strftime("%p"))
print("Date2 day number of year = ",date2.strftime("%j"))
print("Date2 week number of year = ",date2.strftime("%U"))

The output is as follows:

Date1 =  2020-05-31 09:00:48.776317
Date1 weekday =  Sunday
Date1 weekday as number =  0
Date1 day of month =  31
Date1 month name =  May
Date1 month number =  05
Date1 year (without century) =  20
Date1 year (with century) =  2020
Date1 hour (24-hour format) =  09
Date1 hour (12-hour format) =  09
Date1 time in AM/PM =  AM
Date1 day number of year =  152
Date1 week number of year =  22
----------------------------------
Date2 =  2019-08-22 15:20:50
Date2 weekday =  Thursday
Date2 weekday as number =  4
Date2 day of month =  22
Date2 month name =  August
Date2 month number  =  08
Date2 year (without century)  =  19
Date2 year (with century)  =  2019
Date2 hour (24-hour format) =  15
Date2 hour (12-hour format) =  03
Date2 time in AM/PM =  PM
Date2 day number of year =  234
Date2 week number of year =  33

Python time module

For time related manipulation, use the time module in Python. Import the time module and use its method for manipulation:

Get the current time using the time module

To get the current time in Python, at first, import the time module:

import time;

Next, use time() to get the current time as in the below example:

import time;

mytime = time.localtime(time.time())
print "Current time = ", mytime

The output is as follows:

Local current time : time.struct_time(tm_year=2020, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=0, tm_sec=42, tm_wday=5, tm_yday=151, tm_isdst=0

Get the number of seconds passed since epoch

To get the number of seconds since epoch, at first, import the time module:

import time

Note: January 1, 1970, 00:00:00 at UTC is epoch

Next, use the time() method as in the below example:

import time

sec = time.time()
print("No. of Seconds passed since epoch =", sec)

The output is as follows:

Current time =  time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=10, tm_min=19, tm_sec=36, tm_wday=6, tm_yday=152, tm_isdst=0)

Python calendar module

The calendar module in Python is used to perform calendar operations such as displaying calendar of a year, calendar of a month, etc.

Display a calendar for a year

To display calendar of a year, use the Calendar class. Before that, import the calendar module:

import calendar

For calendar, use the calendar() method with parameters: calendar(y, width, line, col)

y = year
width = width of characters
Line: no. of lines
col = column separations

Moving further, let us see an example and display calendar of year 2020:

import calendar 
  
print ("2020 calendar = ") 

#Display 2020 calendar
print (calendar.calendar(2020,2,1,9))

The output is as follows:

Python DateTime Calendar Module Examples

 

Get the calendar for a month

To get the calendar of a month in Python, import the Calendar module:

import calendar

Next, let us see an example to get the calendar of let’s say, June month, year 2020:

import calendar  
    
year = 2020
month = 6
    
print("Calendar of May month - year 2020: "+calendar.month(year, month))

The output is as follows:

The output is as follows:
Calendar of May month - year 2020:      June 2020
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
29 30

How to compare two dates in Python?

To compare two dates in Python, the following comparison operators are used:

<, >, <=, >=

Additionally, we need to use the datetime module:

import datetime

Now, let us see an example to compare two dates:

import datetime 
  
#two dates
dt1 = datetime.datetime(2020, 8, 10) 
dt2 = datetime.datetime(2020, 8, 14) 
  
# Comparing dates
if (dt1 > dt2):
   print("Date1 is greater tha Date2")
elif (dt1 < dt2):
   print("Date2 is greater than Date1")
else:
   print("Both the dates are equal")

The output is as follows:

Date2 is greater than Date1

In this tutorial, we learned about Python datetime, time, calendar & other modules. Additionally, we manipulated date and time with methods & format codes.


Recommended Posts

Python Strings with Examples
Functions in Python with Examples
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment