Python DateTime Tutorial
Python DateTime Tutorial
https://studyopedia.com/python3/python-datetime/ 3/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
import datetime
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)
Current Date and Time = 2020-05-30 11:18:25.100245
from datetime import date
from datetime import date
res = date.today()
print("Getting today's date:", res)
Getting today's date: 2020-05-30
# 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"))
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
#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"))
Date1 = 2020-05-30 19:00:54.272196
Date1 weekday = Saturday
Date2 = 2020-07-15 00:00:00
Date2 weekday = Wednesday
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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
# 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"))
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
For time related manipulation, use the time module in Python. Import the time module and use its method
for manipulation:
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
Local current time : time.struct_time(tm_year=2020, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=0,
import time
import time
sec = time.time()
print("No. of Seconds passed since epoch =", sec)
Current time = time.struct_time(tm_year=2020, tm_mon=5, tm_mday=31, tm_hour=10, tm_min=19, tm_
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))
https://studyopedia.com/python3/python-datetime/ 24/29
3/5/22, 10:58 AM Python DateTime Tutorial with examples - Studyopedia
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:
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
<, >, <=, >=
import datetime
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")
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
} 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
NO COMMENTS
https://studyopedia.com/python3/python-datetime/ 28/29