7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 1/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
Working With Dates & Times
Datetime
Python has a built-in datetime module which provides convenient objects
to work with dates and times.
Code
PYTHON
1 import datetime
Datetime classes
Commonly used classes in the datetime module are:
date class
time class
datetime class
timedelta class
Working with 'date' class
Representing Date
A date object can be used to represent any valid date (year, month and
day).
Code
PYTHON
1 import datetime
2
3 date_object = datetime.date(2019, 4, 13)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 2/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
4 print(date_object)
Output
2019-04-13
Date Object
Code
PYTHON
1 from datetime import date
2 date_obj = date(2022, 2, 31)
3 print(date_obj)
Output
ValueError: day is out of range for month
Today’s Date
Class method
today() returns a date object with today’s date.
Code
PYTHON
1 import datetime
2
3 date_object = datetime.date.today()
4 print(date_object)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 3/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
Output
2021-02-05
Attributes of Date Object
Code
PYTHON
1 from datetime import date
2
3 date_object = date(2019, 4, 13)
4 print(date_object.year)
5 print(date_object.month)
6 print(date_object.day)
Output
2019
4
13
Working with ‘time’ Class
Representing Time
A time object can be used to represent any valid time (hours, minutes and
seconds).
Code
PYTHON
1 from datetime import time
2
3 time_object = time(11, 34, 56)
4 print(time object)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 4/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
4 print(time_object)
Output
11:34:56
Attributes of Time Object
Code
PYTHON
1 from datetime import time
2
3 time_object = time(11, 34, 56)
4 print(time_object)
5 print(time_object.hour)
6 print(time_object.minute)
7 print(time_object.second)
Output
11:34:56
11
34
56
Working with ‘datetime’ Class
Datetime
The datetime class represents a valid date and time together.
Example - 1
Code
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 5/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
PYTHON
1 from datetime import datetime
2
3 date_time_obj = datetime(2018, 11, 28, 10, 15, 26)
4 print(date_time_obj.year)
5 print(date_time_obj.month)
6 print(date_time_obj.hour)
7 print(date_time_obj.minute)
Output
2018
11
10
15
Example - 2
It gives the current date and time
Code
PYTHON
1 import datetime
2
3 datetime_object = datetime.datetime.now()
4 print(datetime_object)
Output
2021-02-05 09:26:08.077473
DateTime object
Code
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 6/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
PYTHON
1 from datetime import datetime
2 date_time_obj = datetime(2018, 11, 28)
3 print(date_time_obj)
Output
2018-11-28 00:00:00
Formatting Datetime
The datetime classes have
strftime(format) method to format the datetime into any required format
like
mm/dd/yyyy
dd-mm-yyyy
Format Specifier Meaning Example
Year without century as a zero-
%y 19, 20, ...
padded decimal number
Year with century as a decimal
%Y 2019, 2020, ...
number
%b Month as abbreviated name Jan, Feb, ...
January,
%B Month as full name
February
Month as a zero-padded decimal
%m 01, 02, …, 12
number
Day of the month as a zero-
%d 01, 02, …, 31
padded decimal number
%a Weekday as abbreviated name Sun, Mon, ...
Sunday,
%A Weekday as full name
Monday, ...
Hour (24-hour clock) as a zero-
%H 00, 01, …, 23
padded decimal number
Hour (12-hour clock) as a zero-
%I 01, 02, …, 12
padded decimal number
%p AM or PM AM, PM
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 7/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
Format Specifier Meaning Example
Minute as a zero-padded decimal
%M 00, 01, …, 59
number
Second as a zero-padded decimal
%S 00, 01, …, 59
number
Code
PYTHON
1 from datetime import datetime
2
3 now = datetime.now()
4 formatted_datetime_1 = now.strftime("%d %b %Y %I:%M:%S
5 print(formatted_datetime_1)
6
7 formatted_datetime_2 = now.strftime("%d/%m/%Y, %H:%M:%
8 print(formatted_datetime_2)
Output
05 Feb 2021 09:26:50 AM
05/02/2021, 09:26:50
Parsing Datetime
The class method
strptime() creates a datetime object from a given string representing
date and time.
Code
PYTHON
1 from datetime import datetime
2
3 date_string = "28 November, 2018"
4 print(date_string)
5
6 date_object = datetime.strptime(date_string, "%d %B, %
7 print(date object)
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 8/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
7 print(date_object)
Output
28 November, 2018
2018-11-28 00:00:00
Working with ‘timedelta’ Class
Timedelta object represents duration.
Example 1
Code
PYTHON
1 from datetime import timedelta
2
3 delta = timedelta(days=365, hours=4)
4 print(delta)
Output
365 days, 4:00:00
Example 2
Code
PYTHON
1 from datetime import timedelta, datetime
2 delta = timedelta(days=365)
3 current_datetime = datetime.now()
4 print(current_datetime)
5 next_year_datetime = current_datetime + delta
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec1… 9/10
7/27/24, 9:55 PM Revolutionizing the Job Market | NxtWave
6 print(next_year_datetime)
Output
2021-02-05 09:28:30.239095
2022-02-05 09:28:30.239095
Calculating Time Difference
Code
PYTHON
1 import datetime
2
3 dt1 = datetime.datetime(2021, 2, 5)
4 dt2 = datetime.datetime(2022, 1, 1)
5 duration = dt2 - dt1
6 print(duration)
7 print(type(duration))
Output
330 days, 0:00:00
<class 'datetime.timedelta'>
https://learning.ccbp.in/course?c_id=a6454e48-d030-4d49-8920-253198052232&t_id=7f553143-459d-43df-a3fb-4749ed732353&s_id=8737ec… 10/10