{"id":21435,"date":"2022-12-16T07:01:00","date_gmt":"2022-12-16T06:01:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=21435"},"modified":"2023-01-16T14:42:30","modified_gmt":"2023-01-16T13:42:30","slug":"python-dates-and-time","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/","title":{"rendered":"Work with Python dates and time using the datetime module"},"content":{"rendered":"\n<p>If you work with dates and time in Python, it can be a bit tricky. But with the right tools, syntaxes and best practices, it becomes much more easy. Dates and times are an essential part of many applications, and working with them in Python can be challenging. However, with the right tools, it can be quite easy.<\/p>\n\n\n\n<p>In this blog post, we will explore the various ways in which we can work with dates and times in Python. We will take a deep dive into the datetime module and learn how to create and manipulate date and time objects. We will also learn how to format dates and times, and how to calculate time differences.<\/p>\n\n\n\n<p>By the end of this Python tutorial, you will have a solid understanding of how to work with dates and times in Python and be able to apply this knowledge to your own projects. In this <a href=\"https:\/\/expert-only.com\/en\/python\/introduction-to-python-programming\/\">Python tutorial<\/a>, we&#8217;ll be covering the 4 main following topics about Python datetime, the basics, the objects, the formatting and the calculation of differences.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#1-basics-of-the-python-datetime-module\" >1. Basics of the Python datetime module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#2-working-with-date-objects-n-python\" >2. Working with date objects n Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#3-format-dates-and-times-in-python\" >3. Format dates and times in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#4-calculate-time-differences-with-python\" >4. Calculate time differences with Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#5-conclusion-on-the-datetime-module-in-python\" >5. Conclusion on the datetime module in Python<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-basics-of-the-python-datetime-module\"><span class=\"ez-toc-section\" id=\"1-basics-of-the-python-datetime-module\"><\/span>1. Basics of the Python datetime module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong>datetime module in Python<\/strong> is used to work with dates and time. It provides several classes to work with dates and times, including : <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>date<\/li>\n\n\n\n<li>time<\/li>\n\n\n\n<li>datetime<\/li>\n\n\n\n<li>timedelta<\/li>\n\n\n\n<li>and tzinfo<\/li>\n<\/ul>\n\n\n\n<p>In order to use the datetime module, import it like in the following script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import datetime\nfrom datetime import date\n\ntoday = date.today()\n\nprint('today')\nprint(today)\n# result: 2023-01-13\n\nbirthday = date(1991, 12, 1)\nprint('birthday')\nprint(birthday)\n# result: 1991-12-01\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"580\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-dates-and-time-datetime-module-example.jpg\" alt=\"Import the datetime module to work with Python dates and time.\" class=\"wp-image-21487\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-dates-and-time-datetime-module-example.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-dates-and-time-datetime-module-example-300x242.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><strong>Import the datetime module to work with Python dates and time.<\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>So dates and times are some of the most tricky <a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">Python data types<\/a> to work with, because it is mandatory to take into considerations the regional parameters and date formats.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"cBVCCG2lkq\"><a href=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/\">Python data types with code examples<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Python data types with code examples&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/python-data-types\/embed\/#?secret=AuMO5uqF9n#?secret=cBVCCG2lkq\" data-secret=\"cBVCCG2lkq\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-working-with-date-objects-n-python\"><\/span>2. Working with date objects n Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>datetime<\/code> module in Python provides a convenient way to work with dates. One of the classes within this module is the <code>date<\/code> class, which is specifically designed for this purpose. Indeed, the <code>date<\/code> class in the <code>datetime<\/code> module provides a simple and intuitive way to work with dates in Python, making it easy to perform various operations.<\/p>\n\n\n\n<p>The date class in the datetime module is used to work with dates. It takes in three different parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Year<\/li>\n\n\n\n<li>Month<\/li>\n\n\n\n<li>Day<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s an another example of how to create a date object using a Python script.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import date\ntoday = date.today()\nprint(today)\n#result: 2022-11-28\n\nbirthday = date(1995, 12, 25)\nprint(birthday)\n# result: 1995-12-25\n\n<\/pre>\n\n\n\n<p>You can also access the year, month, and day of a date object individually. Simply use the code below and adapt it to your needs if need be.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">today.year\n# 2023\n\ntoday.month\n# 01\n\ntoday.day\n# 28\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-format-dates-and-times-in-python\"><\/span>3. Format dates and times in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When working with dates and times in Python, it&#8217;s often necessary to format them in a specific way for display or storage. The <strong>strftime method<\/strong>, which is available on both the <em>date<\/em> and <em>datetime<\/em> classes in the <code>datetime<\/code> module, provides a powerful way to format dates and times.<\/p>\n\n\n\n<p>The strftime method takes in a format string, which specifies how the date or time should be represented. The format string consists of various codes, each of which corresponds to a specific component of the date or time. Here are a few examples of format codes that can be used in a format string:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%Y<\/code>: the four-digit year<\/li>\n\n\n\n<li><code>%m<\/code>: the two-digit month (with a leading zero, if necessary)<\/li>\n\n\n\n<li><code>%d<\/code>: the two-digit day of the month (with a leading zero, if necessary)<\/li>\n\n\n\n<li><code>%H<\/code>: the two-digit hour (with a leading zero, if necessary)<\/li>\n\n\n\n<li><code>%M<\/code>: the two-digit minute (with a leading zero, if necessary)<\/li>\n\n\n\n<li><code>%S<\/code>: the two-digit second (with a leading zero, if necessary)<\/li>\n<\/ul>\n\n\n\n<p>Here is an example of how to use the strftime method to format a date as a string:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import date\nnew_year = date(2020, 1, 1)\n\nformatted_date = new_year.strftime('%Y-%m-%d')\nprint(formatted_date)\n<\/pre>\n\n\n\n<p>You can also use the strftime method with the <code>datetime<\/code> class which also includes the time along with date.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import datetime\nnow = datetime.now()\n\nformatted_datetime = now.strftime('%Y-%m-%d %H:%M:%S')\nprint(formatted_datetime)\n<\/pre>\n\n\n\n<p>In the next section of the tutorial is about how to calculate time differences using Python, another way to do achieve this is to perform these calculations directly in a database. Of course, it applies to projects using databases as back ends, here is how to <a href=\"https:\/\/expert-only.com\/en\/t-sql\/calculate-difference-between-two-dates\/\">calculate time differences using T-SQL scripts<\/a>. <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/expert-only.com\/en\/t-sql\/calculate-difference-between-two-dates\/\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-calculate-time-differences-with-python\"><\/span>4. Calculate time differences with Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Calculating time differences with Python is a simple task that can be accomplished using the timedelta class in the datetime module. The timedelta class allows you to specify the difference between two dates in a number of different units, such as days, seconds, microseconds, milliseconds, minutes, hours, or weeks. This makes it a versatile tool for working with dates and times in your Python code.<\/p>\n\n\n\n<p>Here&#8217;s an example of how you can use the timedelta class to calculate the difference between two dates:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import datetime, timedelta\n\ndate1 = datetime(2022, 1, 1)\ndate2 = datetime(2022, 2, 1)\n\ntime_diff = date2 - date1\nprint(time_diff)\n<\/pre>\n\n\n\n<p>The previous Python script calculates the difference between January 1st, 2022 and February 1st, 2022 and assigns it to the variable time_diff. If you run this code, you will see that the output is <em>31 days, 0:00:00<\/em>, which represents 31 days.<\/p>\n\n\n\n<p>You can also use the timedelta class to perform more <a href=\"https:\/\/www.mathway.com\/Calculator\/complex-number-calculator\" target=\"_blank\" rel=\"noreferrer noopener\">complex calculations<\/a>. For example, you can calculate the difference between two dates in seconds by adding a parameter to the timedelta class:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">date1 = datetime(2022, 1, 1, 12, 0, 0)\ndate2 = datetime(2022, 1, 1, 12, 30, 0)\n\ntime_diff = date2 - date1\nprint(time_diff.total_seconds())\n<\/pre>\n\n\n\n<p>This code calculates the difference between 12:00 PM on January 1st, 2022 and 12:30 PM on the same day and assigns it to the variable <code>time_diff<\/code>. If you run this code, you will see that the output is <code>1800.0<\/code>, which represents 1800 seconds or 30 minutes.<\/p>\n\n\n\n<p>You can also use timedelta to perform simple operations like adding or subtracting time from a datetime object, for example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">date1 = datetime(2023, 1, 1)\ntime_diff = timedelta(days=10)\n\ndate_after_10_days = date1 + time_diff\nprint(date_after_10_days)\n<\/pre>\n\n\n\n<p>The code we just saw adds 10 days to the <em>date1<\/em> variable and assigns the result to other variable called <em>date_after_10_days<\/em>. If you run this code, you will see that the output is <em>2023-01-11 00:00:00<\/em>.<\/p>\n\n\n\n<p>In conclusion, the timedelta class in the datetime module provides a powerful and flexible way to work with dates and times in Python, making it much more easy to perform calculations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-conclusion-on-the-datetime-module-in-python\"><\/span>5. Conclusion on the datetime module in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, this tutorial shows how to work with dates and time in Python. It is made easy with the datetime module, which provides a variety of useful classes and functions for dealing with dates and times. The datetime class provides a way to create and manipulate date and time objects, while the timedelta class allows you to calculate the difference between two dates and perform other operations.<\/p>\n\n\n\n<p>Additionally, the time class allows you to work with time objects and the date class is used to create date objects. Overall, these classes and functions provide a powerful and flexible way to work with dates and times in Python, making it easy to perform calculations and operations with them.<\/p>\n\n\n\n<p>Another very common data type and certainly the most used in terms of data volumes, is the string data type. Here is how to <a href=\"https:\/\/expert-only.com\/en\/python\/python-strings\/\">manage Python strings with scripts examples<\/a> and methods.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"BBNqZA14nR\"><a href=\"https:\/\/expert-only.com\/en\/python\/python-strings\/\">Python Strings<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Python Strings&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/python-strings\/embed\/#?secret=xjQamkf7qE#?secret=BBNqZA14nR\" data-secret=\"BBNqZA14nR\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>If you work with dates and time in Python, it can be a bit tricky. But with the right tools, syntaxes and best practices, it becomes much more easy. Dates and times are an essential part of many applications, <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\" title=\"Work with Python dates and time using the datetime module\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-21435","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Work with Python dates and time - Datetime module - Learn Python<\/title>\n<meta name=\"description\" content=\"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Work with Python dates and time using the datetime module\" \/>\n<meta property=\"og:description\" content=\"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-16T06:01:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-16T13:42:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Work with Python dates and time using the datetime module\",\"datePublished\":\"2022-12-16T06:01:00+00:00\",\"dateModified\":\"2023-01-16T13:42:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\"},\"wordCount\":1109,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\",\"name\":\"Work with Python dates and time - Datetime module - Learn Python\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"datePublished\":\"2022-12-16T06:01:00+00:00\",\"dateModified\":\"2023-01-16T13:42:30+00:00\",\"description\":\"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Work with Python dates and time using the datetime module\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Work with Python dates and time - Datetime module - Learn Python","description":"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/","og_locale":"en_US","og_type":"article","og_title":"Work with Python dates and time using the datetime module","og_description":"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.","og_url":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-12-16T06:01:00+00:00","article_modified_time":"2023-01-16T13:42:30+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Work with Python dates and time using the datetime module","datePublished":"2022-12-16T06:01:00+00:00","dateModified":"2023-01-16T13:42:30+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/"},"wordCount":1109,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/","url":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/","name":"Work with Python dates and time - Datetime module - Learn Python","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","datePublished":"2022-12-16T06:01:00+00:00","dateModified":"2023-01-16T13:42:30+00:00","description":"Create and manipulate Python dates and time objects using the datetime module, like how to create, format and calculate dates and times.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/macro-1452987_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/python-dates-and-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Work with Python dates and time using the datetime module"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=21435"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21435\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10744"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=21435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=21435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=21435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}