Datetime in Python using ISO 8601
You are going to learn how to deal with ISO 8601 datetime in this post if you are using Python. You will get new knowledge after reading this essay about Python.
:
- How to get current ISO 8601 datetime in Python.
- How to convert the existing datetime to ISO 8601 time format.
- Also, datetime can contain timezone information i will show you how to convert datetime with timezone information to ISO 8601 format.
- Also, I will show you how to convert UTC to ISO 8601 format.
Table of contents
-
What is the Format of ISO 8601 Datetime in Python
-
How to get ISO 8601 Datetime in Python
-
Convert Datetime with TimeZone information to ISO 8601
-
UTC to ISO 8601 in Python
-
Local Datetime to ISO 8601 without microsecond
What is the Format of ISO 8601 Datetime in Python
The first question that needs answering is, “What exactly is ISO time?” Dates may be represented using ISO 8601, which is the universally accepted standard. (YYYY-MM-DD). Anybody who wishes to utilize a standardized method of displaying datetime, UTC, and local time with an offset to UTC may use ISO 8601 without any restrictions. Date format according to ISO 8601:
The date is expressed in Python using the ISO 8601 standard using the format YYYY-MM-DDTHH:MM:SS.mmmmmm. The date and time of the 18th of May, 2022, for instance, is written as 2022-05-18T11:40:22.519222. Here
:
-
YYYY
: Year in four-digit format -
MM
: Months from 1-12 -
DD
: Days from 1 to 31 -
T
: It is the separator character that is to be printed between the date and time fields. It is an optional parameter having a default value of “T”. -
HH
: For the value of minutes -
MM
: For the specified value of minutes -
SS
: For the specified value of seconds -
mmmmmm
: For the specified microseconds
How to get ISO 8601 Datetime in Python
There are a variety of circumstances in which you may wish to get the current datetime according to ISO 8601. In addition to this, it is possible that the current datetime has to be converted into an ISO 8601 format. Both of these scenarios will be discussed. The methods that are shown below demonstrate how to convert datetime to an ISO 8601 date in string format.
Python.
-
Import datetime module
Python datetime
module provides various functions to create and manipulate the date and time. Use the
from datetime import datetime
statement to import a
datetime
class from a datetime module. -
Get Current Datetime
If you want to get the current ISO 8601 datetime first, you need to get the
current datetime
using the
datetime.now()
function. If you already have a datetime object, you can skip this step. -
Use the isoformat() method
To convert datetime to ISO 8601 format use the
isoformat()
method. It returns a string representing the date in ISO 8601 format. this ISO string contains the date, time, and UTC offset to the corresponding time zone.
Example 1: Get Current ISO 8601 Datetime
from datetime import datetime
# get current datetime
today = datetime.now()
print('Today Datetime:', today)
# Get current ISO 8601 datetime in string format
iso_date = today.isoformat()
print('ISO DateTime:', iso_date)
Run Output
:
Today Datetime: 2022-05-18 12:19:51.685496 ISO DateTime: 2022-05-18T12:19:51.685496
If you wish to modify the separator that appears between the date and the time, refer to the code below.
time.
from datetime import datetime
iso_date = datetime.now().isoformat('#')
print(iso_date)
# output
# 2022-05-18#12:43:02.430554
Run
Example 2: Convert Datetime to ISO 8601 format
If you have an input datetime object, you may convert it to ISO 8601 format by following the instructions in the following example. Also, if the datetime is stored in a string format, you must first convert the string to datetime format.
.
from datetime import datetime
dt = datetime(2021, 10, 24, 8, 48, 34, 685496)
print('Input Datetime:', dt)
# convert datetime to ISO date
iso_date = dt.isoformat()
print('ISO Date:', iso_date)
Run Output
:
Input Datetime: 2021-10-24 08:48:34.685496 ISO Date: 2021-10-24T08:48:34.685496
Convert Datetime with TimeZone information to ISO 8601
The standardized time is denoted by a time zone, which differs in name depending on where region of the globe is being discussed. For instance, Central Time (CT) in North and South America is either five or six hours behind Coordinated Universal Time (UTC), and is represented as UTC-5 or UTC-6 depending on whether or not Daylight Saving Time is in effect.
A date object may be referenced in Python either with or without the inclusion of time zones. On the basis of this, an object may either be considered Naive or Aware. A date object is presumed to be naïve by default. If it has the timezone value, a datetime or time object is said to be aware. For more information, please refer to the timezone in Python.
For instance, the datetime.now() method provides the current datetime in the local timezone without providing any more information about the time zone. By using the pytz library, we are able to get the current datetime based on the timezone that we choose by passing the timezone name to this method.
In the Python example that follows, we’ll learn how to convert datetime together with information about the time zone into the ISO 8601 date format.
format.
from datetime import datetime
import pytz
# current Datetime
# US/Central timezone datetime
aware_us_central = datetime.now(pytz.timezone('US/Central'))
print('US Central DateTime', aware_us_central)
# timezone aware datetime to ISO 8601 date
iso_date = aware_us_central.isoformat()
print('ISO Datetime', iso_date)
Run Output
:
US Central DateTime 2022-05-18 03:08:36.233201-05:00 ISO Datetime 2022-05-18T03:08:36.233201-05:00
Please take note that the UTC Offset for Central Time in the United States is 05:00.
timezone.
Get current isoformat datetime string including the default timezone
-
Get
current datetime
using the
now()
function -
Next, Add default timezone information to datetime using the
astimezone()
function. The local timezone or default is your system’s timezone information. -
In the end, use the
isoformat()
method to get the current isoformat datetime string including the default timezone.
Example
:
from datetime import datetime, timezone
# get current datetime in UTC
dt = datetime.now(timezone.utc)
# add local timezone information to datetime
tz_dt = dt.astimezone()
print('current datetime with local timezone:', tz_dt)
# Get current iso 8601 format datetime string including the default timezone
iso_date = tz_dt.isoformat()
print('ISO datetime with local timezone:', iso_date)
Run Output
:
current datetime with local timezone: 2022-05-18 14:26:07.827208+05:30 ISO datetime with local timezone: 2022-05-18T14:26:07.827208+05:30
Please take note that my computer is set to the Indian timezone (IST), which is +05.30. You’ll receive a different outcome based on your system’s
timezone.
UTC to ISO 8601 in Python
Coordinated Universal Time, sometimes known as UTC, is the time system that is used everywhere in the globe. It is thus suggested that you use UTC as your base timezone when working with timezones in Python. This will ensure that you do not run into any problems.
The purpose of this example is to demonstrate how to convert a date in UTC to a date in ISO 8601 format.
Python.
-
First, get the current UTC datetime by mentioning the
timezone.utc
attribute in the
now()
function. -
Next, use the
isoformat()
method convert the UTC time to ISO 8601 format.
Example
:
from datetime import datetime, timezone
# get current datetime in UTC
utc_dt = datetime.now(timezone.utc)
print('UTC time:', utc_dt)
# convert UTC time to ISO 8601 format
iso_date = utc_dt.isoformat()
print('ISO datetime:', iso_date)
Run Output
:
UTC time: 2022-05-18 09:32:28.779252+00:00 ISO datetime: 2022-05-18T09:32:28.779252+00:00
Please take note that the offset at the very end is 00:00, which is the standard UTC.
offset.
UTC to ISO 8601 with local timezone information without a microsecond
- Import datetime class from a datetime module
-
Next, get the current datetime using the
now()
function -
Next, use the
astimezone()
to add the local timezone information to a datetime object. -
In the end, use the
isoformat()
method to convert the UTC to ISO 8601 with local timezone information.
Example
:
from datetime import datetime, timezone
# get current datetime in UTC
utc_dt = datetime.now(timezone.utc)
print('UTC time:', utc_dt)
# convert UTC time to ISO 8601 format
iso_date = utc_dt.astimezone().isoformat()
print('ISO datetime:', iso_date)
Run Output
:
ISO datetime: 2022-05-18T13:56:41+05:30
Local Datetime to ISO 8601 without microsecond
Your system’s datetime is the same as your local time. For instance, the datetime.now() method provides the current datetime in the local timezone without providing any more information about the time zone.
If you want to remove the microseconds component from the datetime object, you may do so by using the replace() method of a datetime module. Let’s look at how to convert the local datetime to the ISO 8601 datetime standard without using the microseconds component. Example
:
from datetime import datetime
import pytz
# local datetime to ISO Datetime
iso_date = datetime.now().replace(microsecond=0).isoformat()
print('ISO Datetime:', iso_date)
Run Output
:
ISO Datetime: 2022-05-18T13:43:13
Local to ISO 8601 with TimeZone information
An Illustration:
:
from datetime import datetime
# get local time
dt = datetime.now()
# convert local datetime to ISO 8601 with TimeZone information
iso_date = dt.astimezone().isoformat()
print('ISO datetime with local timezone:', iso_date)
Run Output
:
ISO datetime with local timezone: 2022-05-18T14:08:08.080091+05:30