{"id":36680,"date":"2022-11-19T07:27:03","date_gmt":"2022-11-19T07:27:03","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36680"},"modified":"2023-01-28T15:05:51","modified_gmt":"2023-01-28T15:05:51","slug":"comparing-date-time-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/comparing-date-time-in-python","title":{"rendered":"Comparing Date &#038; Time in Python [Easy Step By Step]"},"content":{"rendered":"\n<p>When one has to analyze data, it leaves no exception. The tool that is to be deployed for the analysis must be compatible with handling the various types of data that can be thrown at it. The data can be in the form of text, fractions, dates, integers, time &#038; so. Python is one of the most sought tools to analyze data and has its own set of modules to deal with this data diversity.<\/p>\n\n\n\n<p>This article shall focus on demonstrating the comparison between different dates &#038; times using python. By different dates &#038; times, the inference is towards the dates &#038; times in different time zones.<\/p>\n\n\n\n<p>Prima face the time may be the same, but when the time zone is factored in, it is a whole different picture. We shall set out to explore that by importing some of the libraries in python \u2013 pytz &#038; <a href=\"https:\/\/www.askpython.com\/python-modules\/python-datetime-module\" data-type=\"post\" data-id=\"6926\">datetime<\/a>. <\/p>\n\n\n\n<p>It can be done by typing the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\nimport pytz\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"175\" height=\"51\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Datetime-Time-Zone-Libraries-Imported.jpg\" alt=\"Datetime Time Zone Libraries Imported\" class=\"wp-image-36689\"\/><figcaption class=\"wp-element-caption\">Datetime Time Zone Libraries Imported<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparing Dates in Python<\/strong><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.askpython.com\/python-modules\/python-datetime-module\" data-type=\"post\" data-id=\"6926\"><strong>module <em>datetime<\/em><\/strong><\/a><em> <\/em>contains both date &#038; time, so we shall only choose the date for comparison in this section. This can be done by extracting only the date from the above module using the below code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import date\n<\/pre><\/div>\n\n\n<p>Now let us try comparing two dates to determine whether they share the same day of the week. We shall first call today\u2019s date using the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.today()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"294\" height=\"53\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Calling-Todays-Date.jpg\" alt=\"Calling Todays Date\" class=\"wp-image-36692\"\/><figcaption class=\"wp-element-caption\">Calling Todays Date<\/figcaption><\/figure>\n\n\n\n<p>What if one chooses to use <em>datetime.today()<\/em> instead of <em>date.today()<\/em>? This shall lead to the following result since we have already chosen only to use <em>date <\/em>from the <em>datetime <\/em>module.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"81\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Datetime-Error.jpg\" alt=\"Datetime Error\" class=\"wp-image-36693\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Datetime-Error.jpg 514w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Datetime-Error-300x47.jpg 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><figcaption class=\"wp-element-caption\">Datetime Error<\/figcaption><\/figure>\n\n\n\n<p>Let us now move on with feeding another date for comparing it with the current date. But exercise caution while typing the entries for months or dates, since Python is not happy with leading zeros in these places. Else the following error shall appear.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"68\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Leading-Zero-Error.jpg\" alt=\"Leading Zero Error\" class=\"wp-image-36697\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Leading-Zero-Error.jpg 684w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Leading-Zero-Error-300x30.jpg 300w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><figcaption class=\"wp-element-caption\">Leading Zero Error<\/figcaption><\/figure>\n\n\n\n<p>So without the leading zeros, the code becomes,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndt_1=datetime.datetime(2020,5,6)\n<\/pre><\/div>\n\n\n<p>Now that we know how to construct a date, let us now compare the two dates using the <em>weekday() <\/em>to see if they share the same weekday or not.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif date.today().weekday()==date(2020,5,6).weekday():\n    print(&#039;Both dates share same weekday&#039;)\nelse:\n    print(&#039;Both dates do not share same weekday&#039;)\n<\/pre><\/div>\n\n\n<p>Executing the above code gives us the below output.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"476\" height=\"131\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Weekdays-of-Two-Dates-Compared.jpg\" alt=\"Weekdays Of Two Dates Compared\" class=\"wp-image-36703\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Weekdays-of-Two-Dates-Compared.jpg 476w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Weekdays-of-Two-Dates-Compared-300x83.jpg 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><figcaption class=\"wp-element-caption\">Weekdays Of Two Dates Compared<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comparing Different Time Zones in Python<\/strong><\/h2>\n\n\n\n<p>In this section, we shall try to compare the difference between the two time zones &#038; verify whether they are the same. For this, we shall use an exclusive command in Python known as the <em>timedelta(). <\/em>Following is its syntax,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntimedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)\n<\/pre><\/div>\n\n\n<p>This command cannot be used right away, rather be imported from the <em>datetime <\/em>library using,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import timedelta\n<\/pre><\/div>\n\n\n<p>The difference in time between any two dates or times used is what <em><a href=\"https:\/\/www.askpython.com\/python-modules\/python-timedelta\" data-type=\"post\" data-id=\"6579\"><strong>timedelta<\/strong><\/a> <\/em>helps in indicating. Let us set it to 3 hours, 30 minutes and assign it to a variable <em>\u2018Td\u2019<\/em> to verify whether the difference between any two selected time zones matches with it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTd=timedelta(hours=3, minutes=30)\n<\/pre><\/div>\n\n\n<p>Now we shall include both time &amp; date using the following code since we were only limited to the date in the previous section.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime\n<\/pre><\/div>\n\n\n<p>Once done, the current times of the Indian time zone &#038; Australian time zone are assigned to the variables <em>\u2018tind\u2019 <\/em>and <em>\u2018taus\u2019 <\/em>respectively.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntind=pytz.timezone(&#039;Asia\/Kolkata&#039;)\ntaus=pytz.timezone(&#039;Australia\/Brisbane&#039;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"371\" height=\"89\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Assigning-timedelta-Time-Zones.jpg\" alt=\"Assigning Timedelta Time Zones\" class=\"wp-image-36710\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Assigning-timedelta-Time-Zones.jpg 371w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Assigning-timedelta-Time-Zones-300x72.jpg 300w\" sizes=\"auto, (max-width: 371px) 100vw, 371px\" \/><figcaption class=\"wp-element-caption\">Assigning Timedelta Time Zones<\/figcaption><\/figure>\n\n\n\n<p>Then using the <em>if <\/em>logic the comparison is done as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif Td == datetime.now(tind)-datetime.now(taus):\n    print(&#039;Difference in Time Zone = 3hr 30min&#039;)\nelse:\n    print(&#039;Difference in Time Zone != 3hr 30min&#039;)\n<\/pre><\/div>\n\n\n<p>The following is the output once the code is executed.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"115\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Different-Time-Zones-Compared.jpg\" alt=\"Different Time Zones Compared\" class=\"wp-image-36712\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Different-Time-Zones-Compared.jpg 448w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/Different-Time-Zones-Compared-300x77.jpg 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><figcaption class=\"wp-element-caption\">Different Time Zones Compared<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on how to compare dates &#038; times from different time zones using the datetime &#038; pytz libraries in Python. Here\u2019s another article that details <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/pandas-isin\" target=\"_blank\" type=\"URL\" id=\"https:\/\/www.askpython.com\/python\/how-to-use-isin-in-pandas\" rel=\"noreferrer noopener\">how to use isin() command in Pandas<\/a>. There are numerous other enjoyable &amp; equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are in looking to level up in Python. <em>Cheers<\/em>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When one has to analyze data, it leaves no exception. The tool that is to be deployed for the analysis must be compatible with handling the various types of data that can be thrown at it. The data can be in the form of text, fractions, dates, integers, time &#038; so. Python is one of [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":36681,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-36680","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36680"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36680\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/36681"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}