The standard installation comes with the Python datetime module that allows you to work with dates and times. This module is shipped with classes that enable you to manipulate dates and times, including:
- date
- time
- datetime
- timedelta
- timezones
date | Represents date without time |
time | This represents a time without a date |
datetime | Combination of date and time |
timedelta | The difference between time, date or datetime objects |
timezones | Specifies the local time zone and Coordinated Universal Time (UTC) |
Working with dates and times
Now, let’s see how to work with date and time classes in the datetime module.
#working with date from datetime import date #date constructor takes year, month, day dt = date(2022,2,20) print(dt) #output - 2022-02-20 #same as dt = date(day=20, month=2, year=2022) print(dt) #output - 2022-02-20 #accessing day, month and year dd = dt.day mm = dt.month yy = dt.year print(dd) print(mm) print(yy) #outputs # 20 # 2 # 2022
The following example demonstrates how to work with time class in the datetime module.
#working with time from datetime import time #time constructor accepts hour, month, second, microsecond and so on tm = time(7,30,45) print(tm) tm = time(hour=2, minute=15, second=59) print(tm) #outputs # 07:30:45 # 02:15:59
Datetime and timedelta classes
While the datetime objects have a combination of date and time, the timedelta class is used to determine the difference between dates and time objects.
from datetime import datetime dt1 = datetime(2022,2,20) print(dt1) dt2 = datetime(2022,2,20,4,20,59) print(dt2)
Now, let’s use the timedelta to represent a given amount of time and then add it to a datetime object.
from datetime import datetime from datetime import timedelta dt = datetime.now() #using timedelta to determine difference td = timedelta(weeks=2, days=10, minutes=30, seconds=10) dd = dt + td print(dd) #2022-10-07 07:54:41.113542
Converting dates and times to string
To convert date and time objects to strings, you can make use of the strftime method. This method takes formatted codes as arguments.
from datetime import datetime from time import time dt = datetime.now() str_date = dt.strftime('%d-%m-%y') print(str_date) #output # 13-09-22
Here are some formatting codes that you can use for formatting date and time objects.
Code | Meaning | Example |
%d | Day of the month | 22 |
%m | Month as number | 12 |
%y | Year, short version | 22 |
%Y | Year, full version | 2022 |
%a | Weekday, short version | Mon |
%A | Weekday, full version | Monday |
%b | Month, short version | Jan |
%B | Month, full version | January |
%H | Hour (24-hour clock) | 23 |
%I | Hour (12-hour clock) | 2 |
%p | AM/PM | |
%M | Minute | |
%S | Second | |
%c | Local date and time | Tue Sep 13 09:53:43 2022 |
%Z | Timezone | UTC/GMT |
%z | UTC offset | +0100 |
Converting strings to date and time objects
You can use the strptime method to convert a string into date and time objects as shown below:
from datetime import datetime dd = '12/12/1999' dt = datetime.strptime(dd, "%d/%m/%Y") print(type(dt))
Aware and Naive date and times
In Python, the concept of dates and times are categorized under the aware and naive datetime objects.
A date or time object is considered aware if it includes timezone information, and naive if it doesn’t include the timezone information.
Since a naive date or time object doesn’t have timezone information, it will be impossible to relate or compare date and time with that of other time zones.
In order to make date and time-aware objects, you have to include timezone information in the constructor class. However, you would need to install pytz module using pip install and import the module to be able to do so.
from datetime import datetime import pytz tz = pytz.UTC dd = datetime(year=2022, month=9, day=7, tzinfo=tz) print(dd) tz = pytz.timezone('US/Eastern') dd = datetime(year=2022, month=9, day=7, tzinfo=tz) print(dd) dd = datetime.now() #localize datetime dd = pytz.timezone('Africa/Accra').localize(dd) print(dd)