22 Jul Python DateTime Tutorial with examples
To work with Dates in Python, import a module datetime. Let us see what datetime module is 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 the current date, fetching month from date, how to fetch week, month number, etc.
The classes available in datetime module are:
Get the current date and time
To get the current date and time in Python, import the datetime module:
1 2 3 |
import datetime |
After that, use the now() built-in function:
1 2 3 |
datetime.now() |
Now, let us see an example and get the current date and time:
1 2 3 4 5 6 |
import datetime res = datetime.datetime.now() print("Current Date and Time = ",res) |
The output is as follows:
1 2 3 |
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:
1 2 3 |
from datetime import date |
Let us now see an example to get today’s date in Python:
1 2 3 4 5 6 |
from datetime import date res = date.today() print("Getting today's date:", res) |
The output is as follows:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 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:
1 2 3 4 5 6 |
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 weekday like Mon, Tue, Wed, etc. in Python, use the %A format code. Let us see an example to get the weekday:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#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:
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 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:
1 2 3 4 5 6 7 8 9 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
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 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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:
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 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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:
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 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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:
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 31 32 |
# 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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:
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 31 32 33 34 |
# 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:
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 |
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:
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 31 32 33 34 35 36 37 |
# 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:
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 |
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:
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 31 32 33 34 35 36 37 38 |
# 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:
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 |
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
To get the current time in Python, at first, import the time module:
1 2 3 |
import time; |
Next, use time() to get the current time as in the below example:
1 2 3 4 5 6 |
import time; mytime = time.localtime(time.time()) print "Current time = ", mytime |
The output is as follows:
1 2 3 |
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:
1 2 3 |
import time |
Note: January 1, 1970, 00:00:00 at UTC is epoch
Next, use the time() method as in the below example:
1 2 3 4 5 6 |
import time sec = time.time() print("No. of Seconds passed since epoch =", sec) |
The output is as follows:
1 2 3 |
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 calendar of a year
To display calendar of a year, use the Calendar class. Before that, import the calendar module:
1 2 3 |
import calendar |
For calendar, use the calendar() method with parameters: calendar(y, width, line, col)
1 2 3 4 5 6 |
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:
1 2 3 4 5 6 7 8 |
import calendar print ("2020 calendar = ") #Display 2020 calendar print (calendar.calendar(2020,2,1,9)) |
The output is as follows:
Get the calendar of a month
To get the calendar of a month in Python, import the Calendar module:
1 2 3 |
import calendar |
Next, let us see an example to get the calendar of let’s say, June month, year 2020:
1 2 3 4 5 6 7 8 |
import calendar year = 2020 month = 6 print("Calendar of May month - year 2020: "+calendar.month(year, month)) |
The output is as follows:
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 3 |
<, >, <=, >= |
Additionally, we need to use the datetime module:
1 2 3 |
import datetime |
Now, let us see an example to compare two dates:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 |
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
No Comments