Convert ISO Date

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, dates can be represented in various formats. One such format is the ISO-8601 format, which is a standard format for representing dates and times. In this challenge, you will be tasked with converting a date from its ISO-8601 representation to a datetime.datetime object.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/date_time("`Date and Time`") subgraph Lab Skills python/comments -.-> lab-13645{{"`Convert ISO Date`"}} python/function_definition -.-> lab-13645{{"`Convert ISO Date`"}} python/importing_modules -.-> lab-13645{{"`Convert ISO Date`"}} python/using_packages -.-> lab-13645{{"`Convert ISO Date`"}} python/standard_libraries -.-> lab-13645{{"`Convert ISO Date`"}} python/date_time -.-> lab-13645{{"`Convert ISO Date`"}} end

Convert ISO Date

Write a function from_iso_date(d) that takes a string d representing a date in ISO-8601 format and returns a datetime.datetime object representing the same date and time.

from datetime import datetime

def from_iso_date(d):
  return datetime.fromisoformat(d)
from_iso_date('2020-10-28T12:30:59.000000') ## 2020-10-28 12:30:59

Summary

In this challenge, you learned how to convert a date from its ISO-8601 representation to a datetime.datetime object in Python. This can be useful when working with dates and times in various applications.

Other Python Tutorials you may like