You are on page 1of 26

3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

Python DateTime Tutorial with examples


To work with Dates in Python, import a module datetime. Let us see what datetime module and how we
can use it to manipulate date and time. Additionally, we will also work around date and time modules and
understand the concept of Python DateTime.

Python datatime module


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

The classes available in datetime module are:

https://studyopedia.com/python3/python-datetime/ 3/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

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:

 
import datetime
 
res = datetime.datetime.now()
print("Current Date and Time = ",res)
 

The output is as follows:

 
Current Date and Time = 2020-05-30 11:18:25.100245
 

Get Today’s Date


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

 
from datetime import date
 

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


https://studyopedia.com/python3/python-datetime/ 4/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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 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
D t 2 2020 04 25 00 00 00
https://studyopedia.com/python3/python-datetime/ 5/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
Date2 =  2020-04-25 00:00:00
Date2 weekday =  Sat
 

Get the weekday (complete) in Python


To get the weekday 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:

https://studyopedia.com/python3/python-datetime/ 6/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
 
# 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)
https://studyopedia.com/python3/python-datetime/ 7/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
( , ,)
 
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)
i t("D t 1 kd " d t 1 t fti ("%A"))
https://studyopedia.com/python3/python-datetime/ 8/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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)
https://studyopedia.com/python3/python-datetime/ 9/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
 
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()
https://studyopedia.com/python3/python-datetime/ 10/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
()
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:

https://studyopedia.com/python3/python-datetime/ 11/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
 
# 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
 
https://studyopedia.com/python3/python-datetime/ 12/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

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
https://studyopedia.com/python3/python-datetime/ 13/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
y
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"))
https://studyopedia.com/python3/python-datetime/ 14/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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
https://studyopedia.com/python3/python-datetime/ 15/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
 
#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
https://studyopedia.com/python3/python-datetime/ 16/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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"))
https://studyopedia.com/python3/python-datetime/ 17/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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)
 
i ("
https://studyopedia.com/python3/python-datetime/ " d ) 18/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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
https://studyopedia.com/python3/python-datetime/ 19/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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"))
i t("D t 2 d f th " d t 2 t fti ("%d"))
https://studyopedia.com/python3/python-datetime/ 20/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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


https://studyopedia.com/python3/python-datetime/ 21/29
3/5/22, 10:58 AM
y Python DateTime Tutorial with examples - Studyopedia

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

Get the current time


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,
 

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


https://studyopedia.com/python3/python-datetime/ 22/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

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_
 

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 calendar of 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
 
https://studyopedia.com/python3/python-datetime/ 23/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
 

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:

https://studyopedia.com/python3/python-datetime/ 24/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

Get calendar of a month


To get 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
https://studyopedia.com/python3/python-datetime/ 25/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

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:

https://studyopedia.com/python3/python-datetime/ 26/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

 
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 Numbers
Type Conversion in Python
Python Strings
Loops in Python
Python Decision Making Statements
Functions in Python with Examples
Python Tuples with Examples
Dictionary in Pyhon
Python Lists

« Python Strings with


Examples
Functions in Python with
Examples »

 } W
Share Print page 0 Likes

https://studyopedia.com/python3/python-datetime/ 27/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia

STUDYOPEDIA EDITORIAL STAFF

We work to create free tutorials for all.

NO COMMENTS

https://studyopedia.com/python3/python-datetime/ 28/29

You might also like