{"id":2140,"date":"2020-01-03T09:16:42","date_gmt":"2020-01-03T09:16:42","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2140"},"modified":"2022-08-06T13:09:32","modified_gmt":"2022-08-06T13:09:32","slug":"python-pendulum-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-pendulum-module","title":{"rendered":"Python Pendulum Module"},"content":{"rendered":"\n<p>Python&#8217;s <code><strong>pendulum module<\/strong><\/code> enables date\/time conversions and manipulations.<\/p>\n\n\n\n<p>It helps the user to work easily with date\/time formats.<\/p>\n\n\n\n<p>This module enables and provides all the functionalities provided by the <strong>pytz<\/strong> module.<\/p>\n\n\n\n<p><strong>Installation of pendulum module through the command-line:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"27\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Pendulum-module-installation.png\" alt=\"Pendulum Module Installation\" class=\"wp-image-2144\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Pendulum-module-installation.png 348w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Pendulum-module-installation-300x23.png 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><figcaption><em>Pendulum Module Installation<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Importing pendulum module:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pendulum<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Display the current time<\/h2>\n\n\n\n<p>The <code><strong>now()<\/strong><\/code> method is used to display the current date-time of a particular zone.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\ntime = pendulum.now()  \nprint(time)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><code><strong>2020-01-03T10:46:59.049040+05:30<\/strong><\/code><\/p>\n\n\n\n<p>The <code><strong>timezone objects<\/strong><\/code> along with the <code><strong>datetime.now()<\/strong><\/code> function is used to avail the current timestamp of different time-zones.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime\nimport pendulum\n\nutc_time = pendulum.timezone(&#039;UTC&#039;)\npst_time = pendulum.timezone(&#039;America\/Los_Angeles&#039;)\nist_time = pendulum.timezone(&#039;Asia\/Calcutta&#039;)\n\n\nprint(&#039;Date Time in UTC =&#039;, datetime.now(utc_time))\nprint(&#039;Date Time in PST =&#039;, datetime.now(pst_time))\nprint(&#039;Date Time in IST =&#039;, datetime.now(ist_time))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"995\" height=\"160\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-timezone.png\" alt=\"Output Timezone\" class=\"wp-image-2146\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-timezone.png 995w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-timezone-300x48.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-timezone-768x123.png 768w\" sizes=\"auto, (max-width: 995px) 100vw, 995px\" \/><figcaption><em>Output-Timezone<\/em><\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Replacing the datetime module by pendulum module<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\n\nutc = pendulum.now(&#039;UTC&#039;)\n\nprint(&#039;Date Time in UTC =&#039;, utc)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><code><strong>Date Time in UTC = 2020-01-03T05:28:43.853647+00:00<\/strong><\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conversion of time-zones<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\nutc = pendulum.now(&#039;UTC&#039;)\nist = utc.in_timezone(&#039;Asia\/Calcutta&#039;)\n\nprint(&#039;Date Time in IST =&#039;, ist)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><code><strong>Date Time in IST = 2020-01-03T11:05:20.756743+05:30<br><\/strong><\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Date-time manipulations<\/h2>\n\n\n\n<p>The pendulum module provides <code><strong>add(<\/strong>)<\/code> and <code><strong>subtract()<\/strong><\/code> functions to manipulate the date and times in terms of year\/month\/hour and hour\/minute\/second.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\n\nd_t = pendulum.datetime(2020, 2, 29)\nd_t.to_datetime_string()\nprint(d_t)\n\ndt_add = d_t.add(years=5)\nprint(dt_add)\ndt_add = d_t.add(months=5)\nprint(dt_add)\ndt_add = d_t.add(days=2)\nprint(dt_add)\ndt_add = d_t.add(weeks=5)\nprint(dt_add)\ndt_add = d_t.add(hours=5)\nprint(dt_add)\ndt_add = d_t.add(minutes=5)\nprint(dt_add)\ndt_add = d_t.add(seconds=5)\nprint(dt_add)\n\ndt_sub = d_t.subtract(years=1)\nprint(dt_sub)\ndt_sub = d_t.subtract(months=5)\nprint(dt_sub)\ndt_sub = d_t.subtract(days=2)\nprint(dt_sub)\ndt_sub = d_t.subtract(weeks=5)\nprint(dt_sub)\ndt_sub = d_t.subtract(hours=5)\nprint(dt_sub)\ndt_sub = d_t.subtract(minutes=5)\nprint(dt_sub)\ndt_sub = d_t.subtract(seconds=5)\nprint(dt_sub)\n\n\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1244\" height=\"403\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-datetime-manipulations.png\" alt=\"Output Datetime Manipulations\" class=\"wp-image-2157\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-datetime-manipulations.png 1244w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-datetime-manipulations-300x97.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-datetime-manipulations-1024x332.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-datetime-manipulations-768x249.png 768w\" sizes=\"auto, (max-width: 1244px) 100vw, 1244px\" \/><figcaption><em>Output-Datetime Manipulations<\/em><\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">delta() function<\/h2>\n\n\n\n<p>The <code><strong>delta()<\/strong><\/code> function provides the difference between the two timestamps.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\n\nd_t1 = pendulum.datetime(2020, 2, 20)\nd_t1.to_datetime_string()\nprint(d_t1)\nd_t2 = pendulum.datetime(2020, 2, 10)\nd_t2.to_datetime_string()\nprint(d_t2)\n\ndelta = d_t1 - d_t2\n\nprint(delta.start)  \nprint(delta.end)   \n\nprint(delta.in_days())      # 10\nprint(delta.in_hours())     # 240\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1051\" height=\"182\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-delta-function.png\" alt=\"Output Delta Function\" class=\"wp-image-2159\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-delta-function.png 1051w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-delta-function-300x52.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-delta-function-1024x177.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/Output-delta-function-768x133.png 768w\" sizes=\"auto, (max-width: 1051px) 100vw, 1051px\" \/><figcaption>Output Delta Function<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Formatting date-time<\/h2>\n\n\n\n<p>The <code><strong>strftime()<\/strong><\/code> function enables the user to format the date-time in our own format.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\n\nutc = pendulum.now(&#039;UTC&#039;)\n\nprint(utc .to_iso8601_string())\nprint(utc .to_formatted_date_string())\nprint(utc .to_w3c_string())\nprint(utc .to_date_string())\n\n# supports strftime() too\nprint(utc .strftime(&#039;%Y-%m-%d %H:%M:%S %Z%z&#039;))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2020-01-03T07:17:28.650601Z\nJan 03, 2020\n2020-01-03T07:17:28+00:00\n2020-01-03\n2020-01-03 07:17:28 UTC+0000<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison of Dates<\/h2>\n\n\n\n<p>Simple comparison of <code><strong>time-zones<\/strong><\/code> is offered by the pendulum module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pendulum\n\nfirst = pendulum.datetime(2012, 9, 5, 23, 26, 11, 0, tz=&#039;America\/Toronto&#039;)\nsecond = pendulum.datetime(2019, 9, 5, 20, 26, 11, 0, tz=&#039;America\/Vancouver&#039;)\n\nfirst.to_datetime_string()\n\nprint(first.timezone_name)\n\nsecond.to_datetime_string()\n\nprint(second.timezone_name)\n\nprint(first == second)\n\nprint(first != second)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>America\/Toronto\nAmerica\/Vancouver\nFalse\nTrue<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the functionalities offered by the pendulum module.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python pendulum module<\/li><li><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/pendulum.eustace.io\/docs\/\" target=\"_blank\" rel=\"noopener\">Pendulum Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s pendulum module enables date\/time conversions and manipulations. It helps the user to work easily with date\/time formats. This module enables and provides all the functionalities provided by the pytz module. Installation of pendulum module through the command-line: Importing pendulum module: Display the current time The now() method is used to display the current date-time [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2140","post","type-post","status-publish","format-standard","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2140","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2140"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2140\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}