{"id":11063,"date":"2022-10-10T15:09:18","date_gmt":"2022-10-10T09:39:18","guid":{"rendered":"https:\/\/pynative.com\/?p=11063"},"modified":"2022-10-10T15:13:24","modified_gmt":"2022-10-10T09:43:24","slug":"python-compare-dates","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-compare-dates\/","title":{"rendered":"Python Compare Two Dates"},"content":{"rendered":"\n<p>In this lesson, you&#8217;ll learn how to compare two dates or datetime in Python.<\/p>\n\n\n\n<p><strong>You&#8217;ll learn how to<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Check if a given date is greater or less than another date.<\/li><li>Compare two timestamps<\/li><li>Compare two date string<\/li><\/ul>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-how-to-compare-two-dates-in-python\" data-level=\"2\">How to Compare Two Dates in Python<\/a><ul><li><a href=\"#h-example-1-compare-two-datetime\" data-level=\"3\">Example 1: Compare Two DateTime<\/a><\/li><li><a href=\"#h-example-2-compare-two-dates\" data-level=\"3\">Example 2: Compare Two Dates<\/a><\/li><li><a href=\"#h-example-3-compare-times-of-two-datetime-objects\" data-level=\"3\">Example 3: Compare Times of two DateTime Objects<\/a><\/li><\/ul><\/li><li><a href=\"#h-compare-two-date-string\" data-level=\"2\">Compare Two Date String<\/a><\/li><li><a href=\"#h-compare-two-timestamps-in-python\" data-level=\"2\">Compare Two Timestamps in Python<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-compare-two-dates-in-python\">How to Compare Two Dates in Python<\/h2>\n\n\n\n<div class=\"schema-how-to wp-block-yoast-how-to-block\"><p class=\"schema-how-to-description\">Use <a href=\"https:\/\/pynative.com\/python-operators\/#h-relational-comparison-operators\">comparison operators<\/a> (like &lt;, >, &lt;=, >=, !=, etc.) to compare dates in Python. Let\u2019s see the steps to compare dates with the help of the datetime module.<\/p> <ol class=\"schema-how-to-steps\"><li class=\"schema-how-to-step\" id=\"how-to-step-1665134619368\"><strong class=\"schema-how-to-step-name\">import datetime module<\/strong> <p class=\"schema-how-to-step-text\"><a href=\"https:\/\/pynative.com\/python\/datetime\/\">Python datetime\u00a0<\/a>module provides various functions to create and manipulate the date and time. Use the\u00a0<code>from datetime import datetime<\/code>\u00a0statement to import a\u00a0<code>datetime<\/code>\u00a0class from a datetime module.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1665134695433\"><strong class=\"schema-how-to-step-name\"><strong>Convert date string to a datetime object<\/strong><\/strong> <p class=\"schema-how-to-step-text\">If dates are in a string format, we need to convert both date\u00a0<a href=\"https:\/\/pynative.com\/python-string-to-datetime-strptime\/\">strings to a datetime<\/a>\u00a0object before comparing them.<br\/>Use the\u00a0<code>strptime(date_str, format)<\/code>\u00a0function to convert a date string into a datetime object as per the corresponding\u00a0<code>format<\/code>. For example, the\u00a0<code>%Y\/%m\/%d<\/code>\u00a0format codes are for\u00a0<code>yyyy-mm-dd<\/code>.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1665134729587\"><strong class=\"schema-how-to-step-name\">Compare two datetime objects<\/strong> <p class=\"schema-how-to-step-text\">Use <a href=\"https:\/\/pynative.com\/python-operators\/#h-relational-comparison-operators\">comparison operators<\/a> (like <code>&lt;<\/code>, <code>><\/code>, <code>&lt;=<\/code>, <code>>=<\/code>, <code>!=<\/code>, etc.) to compare dates in Python. For example, <code>datetime_1 > datetime_2<\/code> to check if a datetime_1 is greater than datetime_2.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1665387016321\"><strong class=\"schema-how-to-step-name\">Compare two dates<\/strong> <p class=\"schema-how-to-step-text\">If you want to compare only the dates of the DateTime object, use the <strong><code>date()<\/code> method<\/strong> to extract only the date part from the datetime object.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1665387169335\"><strong class=\"schema-how-to-step-name\">Compare two time<\/strong> <p class=\"schema-how-to-step-text\">To compare only the time of the DateTime object, use the <strong><code>time()<\/code> method<\/strong> to extract only the time part from the datetime object.<\/p> <\/li><\/ol><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-1-compare-two-datetime\">Example 1: Compare Two DateTime<\/h3>\n\n\n\n<p>In this example, we&#8217;ll check:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the datetime_1 is greater than another datetime.<\/li><li>If the datetime_1 is lower than datetime_2.<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">compare_dates<\/span><span class=\"hljs-params\">(date1, date2)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">'Date1:'<\/span>, date1)\n    print(<span class=\"hljs-string\">'Date2:'<\/span>, date2)\n\n    <span class=\"hljs-keyword\">if<\/span> date1 &gt; date2:\n        print(<span class=\"hljs-string\">'Date1 is greater than date2'<\/span>)\n    <span class=\"hljs-keyword\">else<\/span>:\n        print(<span class=\"hljs-string\">'Date1 is lower than date2'<\/span>)\n\ndt1 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">13<\/span>)\n<span class=\"hljs-comment\"># today's datetime<\/span>\ntoday = datetime.now()\ncompare_dates(dt1, today)\n\ndt1 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">9<\/span>, <span class=\"hljs-number\">13<\/span>)\ndt2 = datetime(<span class=\"hljs-number\">2021<\/span>, <span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">23<\/span>)\ncompare_dates(dt1, dt2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date1: 2022-09-13 00:00:00\nDate2: 2022-10-07 15:12:52.970943\nDate1 is lower than date2\n\nDate1: 2022-09-13 00:00:00\nDate2: 2021-11-23 00:00:00\nDate1 is greater than date2<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-2-compare-two-dates\">Example 2: Compare Two Dates<\/h3>\n\n\n\n<p>Let&#8217;s assume you have two datetime objects. One only has the date, and the other one has the date &amp; time parts, and you want to compare the dates only (and not the time).<\/p>\n\n\n\n<p>Now, If you want to compare only the dates of DateTime objects, just use the <strong><code>date()<\/code> method<\/strong> to extract only the date part from the datetime object.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-comment\"># input dates<\/span>\nd1 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">27<\/span>).date()\nd2 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">27<\/span>).date()\nd3 = datetime(<span class=\"hljs-number\">2021<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">14<\/span>).date()\nd4 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>).date()\n\n<span class=\"hljs-comment\"># omit .date() function if you want to compare the entire datetime object<\/span>\n<span class=\"hljs-comment\"># use .date() id you want to compare only the date part of it<\/span>\n\n<span class=\"hljs-comment\"># date equality check<\/span>\nprint(<span class=\"hljs-string\">'Date_1:'<\/span>, d1, <span class=\"hljs-string\">'Date_2:'<\/span>, d2, <span class=\"hljs-string\">'Date3:'<\/span>, d3)\nprint(<span class=\"hljs-string\">'Date_1 and Date_2 are equal:'<\/span>, d1 == d2)\nprint(<span class=\"hljs-string\">'Date_1 and Date_3 are equal:'<\/span>, d1 == d3)\n\n<span class=\"hljs-comment\"># check if date is greater than another date<\/span>\nprint(<span class=\"hljs-string\">'Date_1:'<\/span>, d1, <span class=\"hljs-string\">'Date_3:'<\/span>, d3, <span class=\"hljs-string\">'Date_4:'<\/span>, d4)\nprint(<span class=\"hljs-string\">'date_1 is greater than date_3:'<\/span>, d1 &gt; d3)\nprint(<span class=\"hljs-string\">'date_1 is greater than date_4:'<\/span>, d1 &gt; d4)\n\n<span class=\"hljs-comment\"># check if date is less than another date<\/span>\nprint(<span class=\"hljs-string\">'Date_1:'<\/span>, d1, <span class=\"hljs-string\">'Date_3:'<\/span>, d3, <span class=\"hljs-string\">'Date_4:'<\/span>, d4)\nprint(<span class=\"hljs-string\">'date_1 is less than date_3:'<\/span>, d1 &lt; d3)\nprint(<span class=\"hljs-string\">'date_1 is less than date_4:'<\/span>, d1 &lt; d4)\n\n<span class=\"hljs-comment\"># date not equal to check<\/span>\nprint(<span class=\"hljs-string\">'Date_1:'<\/span>, d1, <span class=\"hljs-string\">'Date_2:'<\/span>, d2, <span class=\"hljs-string\">'Date__3:'<\/span>, d3)\nprint(<span class=\"hljs-string\">'Date_1 and Date_2 are NOT equal:'<\/span>, d1 != d2)\nprint(<span class=\"hljs-string\">'Date_1 and Date_3 are NOT equal:'<\/span>, d1 != d3)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date_1: 2022-06-27 Date_2: 2022-06-27 Date3: 2021-05-14\nDate_1 and Date_2 are equal: True\nDate_1 and Date_3 are equal: False\n\nDate_1: 2022-06-27 Date_3: 2021-05-14 Date_4: 2022-10-10\ndate_1 is greater than date_3: True\ndate_1 is greater than date_4: False\n\nDate_1: 2022-06-27 Date_3: 2021-05-14 Date_4: 2022-10-10\ndate_1 is less than date_3: False\ndate_1 is less than date_4: True\n\nDate_1: 2022-06-27 Date_2: 2022-06-27 Date__3: 2021-05-14\nDate_1 and Date_2 are NOT equal: False\nDate_1 and Date_3 are NOT equal: True<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-3-compare-times-of-two-datetime-objects\">Example 3: Compare Times of two DateTime Objects<\/h3>\n\n\n\n<p>Let&#8217;s assume you have two datetime objects. One only has the time, and the other one has the date &amp; time parts, and you want to compare the times only (and not the date).<\/p>\n\n\n\n<p>Use the <strong><code>time()<\/code> method<\/strong> to extract only the time part from the datetime object.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-comment\"># date in yyyy\/-mm-dd hh:mm:ss format<\/span>\ndt_1 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">27<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">45<\/span>, <span class=\"hljs-number\">53<\/span>)\ndt_2 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">27<\/span>, <span class=\"hljs-number\">21<\/span>, <span class=\"hljs-number\">12<\/span>, <span class=\"hljs-number\">34<\/span>)\n\n<span class=\"hljs-comment\"># use .time() if you want to compare only the time part of it<\/span>\n\n<span class=\"hljs-comment\"># date equality check<\/span>\nprint(<span class=\"hljs-string\">'Time_1:'<\/span>, dt_1.time(), <span class=\"hljs-string\">'Time_2:'<\/span>, dt_2.time())\n\nprint(<span class=\"hljs-string\">'Both times are equal:'<\/span>, dt_1.time() == dt_2.time())\nprint(<span class=\"hljs-string\">'Times not equal:'<\/span>, dt_1.time() != dt_2.time())\n\nprint(<span class=\"hljs-string\">'Time_1 is greater than Time_2:'<\/span>, dt_1.time() &gt; dt_2.time())\nprint(<span class=\"hljs-string\">'Time_1 is less than Time_2:'<\/span>, dt_1.time() &lt; dt_2.time())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Time_1: 18:45:53 Time_2: 21:12:34\n\nBoth times are equal: False\nTimes not equal: True\n\nTime_1 is greater than Time_2: False\nTime_1 is less than Time_2: True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-compare-two-date-string\">Compare Two Date String<\/h2>\n\n\n\n<p>There may be a case in which dates are in a string format. Before comparing them, we need to convert both date&nbsp;<a href=\"https:\/\/pynative.com\/python-string-to-datetime-strptime\/\">strings to a datetime<\/a>&nbsp;object. <\/p>\n\n\n\n<p>Use the&nbsp;<code>strptime(date_str, format)<\/code>&nbsp;function to convert a date string into a datetime object as per the corresponding&nbsp;<code>format<\/code>. The format codes are standard directives for mentioning the string format for parsing. For example, the&nbsp;<code>%Y\/%m\/%d<\/code>&nbsp;format codes are for&nbsp;<code>yyyy-mm-dd<\/code>.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">compare_dates<\/span><span class=\"hljs-params\">(date1, date2)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># convert string to date<\/span>\n    dt_obj1 = datetime.strptime(date1, <span class=\"hljs-string\">\"%Y-%m-%d %H:%M:%S\"<\/span>)\n    dt_obj2 = datetime.strptime(date2, <span class=\"hljs-string\">\"%Y-%m-%d %H:%M:%S\"<\/span>)\n    print(<span class=\"hljs-string\">'Date1:'<\/span>, dt_obj1)\n    print(<span class=\"hljs-string\">'Date2:'<\/span>, dt_obj2)\n\n    <span class=\"hljs-keyword\">if<\/span> dt_obj1 == dt_obj2:\n        print(<span class=\"hljs-string\">'Both dates are equal'<\/span>)\n    <span class=\"hljs-keyword\">elif<\/span> dt_obj1 &gt; dt_obj2:\n        print(<span class=\"hljs-string\">'Date1 is greater than date2'<\/span>)\n    <span class=\"hljs-keyword\">else<\/span>:\n        print(<span class=\"hljs-string\">'Date1 is lower than date2'<\/span>)\n\n<span class=\"hljs-comment\"># datetime in yyyy-mm-dd hh:mm:ss format<\/span>\ndt_str1 = <span class=\"hljs-string\">'2022-10-29 8:32:49'<\/span>\ndt_str2 = <span class=\"hljs-string\">'2022-5-7 4:14:58'<\/span>\ncompare_dates(dt1_str1, dt2_str2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date1: 2022-10-29 08:32:49\nDate2: 2022-05-07 04:14:58\nDate1 is greater than date2<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-compare-two-timestamps-in-python\">Compare Two Timestamps in Python<\/h2>\n\n\n\n<p>A timestamp is encoded information generally used in UNIX, which indicates the date and time at which a particular event has occurred. This information could be accurate to the microseconds. It is a POSIX timestamp corresponding to the datetime instance.<\/p>\n\n\n\n<p>To compare <a href=\"https:\/\/pynative.com\/python-timestamp\/\">Python timestamp<\/a> objects, we can use the same conditional operator we used to compare dates.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-comment\"># input timestamp<\/span>\nts1 = datetime(<span class=\"hljs-number\">2022<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">27<\/span>, <span class=\"hljs-number\">18<\/span>, <span class=\"hljs-number\">45<\/span>, <span class=\"hljs-number\">32<\/span>).timestamp()\n<span class=\"hljs-comment\"># get current timestamp<\/span>\nts2 = datetime.now().timestamp()\n\nprint(<span class=\"hljs-string\">'Timestamp_1:'<\/span>, ts1, <span class=\"hljs-string\">'Timestamp_2:'<\/span>, ts2)\n<span class=\"hljs-comment\"># timestamp equality check<\/span>\nprint(<span class=\"hljs-string\">'Timestamp_1 and Timestamp_2 are equal:'<\/span>, ts1 == ts2)\n\n<span class=\"hljs-comment\"># check if timestamp_1 is greater than another timestamp<\/span>\nprint(<span class=\"hljs-string\">'Timestamp_2 is greater than Timestamp_1:'<\/span>, ts2 &gt; ts1)\n\n<span class=\"hljs-comment\"># check if timestamp_1 is less than another timestamp<\/span>\nprint(<span class=\"hljs-string\">'Timestamp_1 is less than Timestamp_2:'<\/span>, ts1 &lt; ts2)\n\n<span class=\"hljs-comment\"># timestamp not equal to check<\/span>\nprint(<span class=\"hljs-string\">'Timestamp_1 is NOT equal to Timestamp_2:'<\/span>, ts1 != ts2)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Timestamp_1: 1656335732.0 Timestamp_2: 1665393979.484924\n\nTimestamp_1 and Timestamp_2 are equal: False\nTimestamp_2 is greater than Timestamp_1: True\nTimestamp_1 is less than Timestamp_2: True\nTimestamp_1 is NOT equal to Timestamp_2: True<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you&#8217;ll learn how to compare two dates or datetime in Python. You&#8217;ll learn how to: Check if a given date is greater or less than another date. Compare two timestamps Compare two date string How to Compare Two Dates in Python Example 1: Compare Two DateTime In this example, we&#8217;ll check: If [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,35],"tags":[38],"class_list":{"0":"post-11063","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-datetime","8":"tag-date-time","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Compare Two Dates [3 Ways] &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-compare-dates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Compare Two Dates\" \/>\n<meta property=\"og:description\" content=\"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-compare-dates\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-10T09:39:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-10-10T09:43:24+00:00\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python Compare Two Dates\",\"datePublished\":\"2022-10-10T09:39:18+00:00\",\"dateModified\":\"2022-10-10T09:43:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/\"},\"wordCount\":558,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"keywords\":[\"DateTime\"],\"articleSection\":[\"Python\",\"Python DateTime\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/\",\"name\":\"Python Compare Two Dates [3 Ways] &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"datePublished\":\"2022-10-10T09:39:18+00:00\",\"dateModified\":\"2022-10-10T09:43:24+00:00\",\"description\":\"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python DateTime\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/datetime\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python Compare Two Dates\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]},{\"@type\":\"HowTo\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#howto-1\",\"name\":\"Python Compare Two Dates\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#article\"},\"description\":\"Use <a href=\\\"https:\\\/\\\/pynative.com\\\/python-operators\\\/#h-relational-comparison-operators\\\">comparison operators<\\\/a> (like &lt;, >, &lt;=, >=, !=, etc.) to compare dates in Python. Let\u2019s see the steps to compare dates with the help of the datetime module.\",\"step\":[{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#how-to-step-1665134619368\",\"name\":\"import datetime module\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Python datetime\u00a0module provides various functions to create and manipulate the date and time. Use the\u00a0from datetime import datetime\u00a0statement to import a\u00a0datetime\u00a0class from a datetime module.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#how-to-step-1665134695433\",\"name\":\"Convert date string to a datetime object\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"If dates are in a string format, we need to convert both date\u00a0strings to a datetime\u00a0object before comparing them.Use the\u00a0strptime(date_str, format)\u00a0function to convert a date string into a datetime object as per the corresponding\u00a0format. For example, the\u00a0%Y\\\/%m\\\/%d\u00a0format codes are for\u00a0yyyy-mm-dd.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#how-to-step-1665134729587\",\"name\":\"Compare two datetime objects\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Use comparison operators (like , =, !=, etc.) to compare dates in Python. For example, datetime_1 > datetime_2 to check if a datetime_1 is greater than datetime_2.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#how-to-step-1665387016321\",\"name\":\"Compare two dates\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"If you want to compare only the dates of the DateTime object, use the date() method to extract only the date part from the datetime object.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-compare-dates\\\/#how-to-step-1665387169335\",\"name\":\"Compare two time\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"To compare only the time of the DateTime object, use the time() method to extract only the time part from the datetime object.\"}]}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Compare Two Dates [3 Ways] &#8211; PYnative","description":"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings","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:\/\/pynative.com\/python-compare-dates\/","og_locale":"en_US","og_type":"article","og_title":"Python Compare Two Dates","og_description":"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings","og_url":"https:\/\/pynative.com\/python-compare-dates\/","og_site_name":"PYnative","article_published_time":"2022-10-10T09:39:18+00:00","article_modified_time":"2022-10-10T09:43:24+00:00","author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-compare-dates\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-compare-dates\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python Compare Two Dates","datePublished":"2022-10-10T09:39:18+00:00","dateModified":"2022-10-10T09:43:24+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-compare-dates\/"},"wordCount":558,"commentCount":0,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"keywords":["DateTime"],"articleSection":["Python","Python DateTime"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-compare-dates\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-compare-dates\/","url":"https:\/\/pynative.com\/python-compare-dates\/","name":"Python Compare Two Dates [3 Ways] &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"datePublished":"2022-10-10T09:39:18+00:00","dateModified":"2022-10-10T09:43:24+00:00","description":"Learn to compare two dates or datetime in Python. Also, learn to compare two timestamps and two date strings","breadcrumb":{"@id":"https:\/\/pynative.com\/python-compare-dates\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-compare-dates\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-compare-dates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Python DateTime","item":"https:\/\/pynative.com\/python\/datetime\/"},{"@type":"ListItem","position":4,"name":"Python Compare Two Dates"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]},{"@type":"HowTo","@id":"https:\/\/pynative.com\/python-compare-dates\/#howto-1","name":"Python Compare Two Dates","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-compare-dates\/#article"},"description":"Use <a href=\"https:\/\/pynative.com\/python-operators\/#h-relational-comparison-operators\">comparison operators<\/a> (like &lt;, >, &lt;=, >=, !=, etc.) to compare dates in Python. Let\u2019s see the steps to compare dates with the help of the datetime module.","step":[{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-compare-dates\/#how-to-step-1665134619368","name":"import datetime module","itemListElement":[{"@type":"HowToDirection","text":"Python datetime\u00a0module provides various functions to create and manipulate the date and time. Use the\u00a0from datetime import datetime\u00a0statement to import a\u00a0datetime\u00a0class from a datetime module."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-compare-dates\/#how-to-step-1665134695433","name":"Convert date string to a datetime object","itemListElement":[{"@type":"HowToDirection","text":"If dates are in a string format, we need to convert both date\u00a0strings to a datetime\u00a0object before comparing them.Use the\u00a0strptime(date_str, format)\u00a0function to convert a date string into a datetime object as per the corresponding\u00a0format. For example, the\u00a0%Y\/%m\/%d\u00a0format codes are for\u00a0yyyy-mm-dd."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-compare-dates\/#how-to-step-1665134729587","name":"Compare two datetime objects","itemListElement":[{"@type":"HowToDirection","text":"Use comparison operators (like , =, !=, etc.) to compare dates in Python. For example, datetime_1 > datetime_2 to check if a datetime_1 is greater than datetime_2."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-compare-dates\/#how-to-step-1665387016321","name":"Compare two dates","itemListElement":[{"@type":"HowToDirection","text":"If you want to compare only the dates of the DateTime object, use the date() method to extract only the date part from the datetime object."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-compare-dates\/#how-to-step-1665387169335","name":"Compare two time","itemListElement":[{"@type":"HowToDirection","text":"To compare only the time of the DateTime object, use the time() method to extract only the time part from the datetime object."}]}],"inLanguage":"en-US"}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/11063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=11063"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/11063\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=11063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=11063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=11063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}