{"id":10901,"date":"2022-05-27T16:43:28","date_gmt":"2022-05-27T11:13:28","guid":{"rendered":"https:\/\/pynative.com\/?p=10901"},"modified":"2022-05-27T16:45:26","modified_gmt":"2022-05-27T11:15:26","slug":"python-iso-8601-datetime","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-iso-8601-datetime\/","title":{"rendered":"Python ISO 8601 Datetime"},"content":{"rendered":"\n
In this article, you\u2019ll learn how to work with ISO 8601 datetime in Python.<\/p>\n\n\n\n
After reading this Python article, you\u2019ll learn<\/strong>:<\/p>\n\n\n\n The first question is, what is ISO time?<\/p>\n\n\n\n ISO 8601<\/a> is an internationally agreed way to represent dates. (YYYY-MM-DD). ISO 8601 can be used by anyone who wants to use a standardized way of presenting datetime, UTC, and local time with offset to UTC.<\/p>\n\n\n\n Format of ISO 8601 Date<\/strong>:<\/p>\n\n\n\n In Python ISO 8601 date is represented in Here<\/strong>:<\/p>\n\n\n\n There can be scenarios where you want to get the current ISO 8601 datetime. Also, you may need to convert the existing datetime to an ISO 8601 format. we’ll cover both the cases. The below steps show how to convert datetime to ISO 8601 date in string format in Python.<\/p> Python datetime\u00a0<\/a>module provides various functions to create and manipulate the date and time. Use the\u00a0 If you want to get the current ISO 8601 datetime first, you need to get the current datetime<\/a> using the To convert datetime to ISO 8601 format use the isoformat()<\/a> 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.<\/p> <\/li><\/ol><\/div>\n\n\n\nTable of contents<\/h2>
What is the Format of ISO 8601 Datetime in Python<\/h2>\n\n\n\n
YYYY-MM-DDTHH:MM:SS.mmmmmm<\/code> format. For example, May 18, 2022, is represented as 2022-05-18T11:40:22.519222<\/strong>.<\/p>\n\n\n\n
How to get ISO 8601 Datetime in Python<\/h2>\n\n\n\n
from datetime import datetime<\/code>\u00a0statement to import a\u00a0
datetime<\/code>\u00a0class from a datetime module.<\/p> <\/li>
datetime.now()<\/code> function. If you already have a datetime object, you can skip this step.<\/p> <\/li>
Example 1: Get Current ISO 8601 Datetime<\/h3>\n\n\n