{"id":7004,"date":"2019-01-10T22:08:14","date_gmt":"2019-01-10T16:38:14","guid":{"rendered":"https:\/\/techbeamers.com\/?p=7004"},"modified":"2025-11-30T10:50:58","modified_gmt":"2025-11-30T15:50:58","slug":"python-datetime","status":"publish","type":"post","link":"https:\/\/techbeamers.com\/python-datetime\/","title":{"rendered":"Python Datetime Module"},"content":{"rendered":"\n<p>This tutorial explains how to work with the date and time values using Python datetime. This module provides classes for representing and manipulating dates and times.<\/p>\n\n\n\n<p>Please note that this module and its functions could come in quite handy for a variety of applications such as:<\/p>\n\n\n\n<p>-Display dates and times on web pages and perform date- and time-related calculations.<br>-Analyze and visualize data that contains dates and times.<br>-Automate tasks that require working with dates and times.<br>-Calculate financial metrics that depend on dates and times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-date-and-time-in-python\"><a id=\"post-7807-_wyohjqqvyiaw\"><\/a>How to Use Date and Time in Python<\/h2>\n\n\n\n<p>Greetings, hope you had a great day. Today, we plan to educate you on how to find the date, day, time, etc. using a module known as the <code>datetime<\/code> in Python.<\/p>\n\n\n\n<p>You should be comfortable with modules in Python before studying the concept of date and time objects in Python. This tutorial uses syntax for Python3 which can be modified for use with Python2.<\/p>\n\n\n\n<p><strong>Pre-requisites<\/strong><\/p>\n\n\n\n<p>You need to have some level of experience using the <a href=\"https:\/\/techbeamers.com\/python-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">modules in Python<\/a>. They provide readymade functions that you can directly call from any program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-introduction-to-datetime-module\">Introduction to Datetime Module<\/h3>\n\n\n\n<p>Although we can use the time module to find out the current date and time, it lacks features such as the ability to create Date objects for a particular manipulation.<\/p>\n\n\n\n<p>To remedy this, Python has a built-in datetime module. Hence, to do any object manipulation regarding date and time, we need to import <code>datetime<\/code> module.<\/p>\n\n\n\n<p>The <code>datetime<\/code> module is used to modify date and time objects in various ways. It contains five classes to manipulate date and time. They are shown in the below table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Python Datetime Classes<\/th><th>Short Description<\/th><\/tr><\/thead><tbody><tr><td><strong><code>date<\/code><\/strong><\/td><td>This class deals with date objects.<\/td><\/tr><tr><td><strong><code>time<\/code><\/strong><\/td><td>It works with time objects.<\/td><\/tr><tr><td><strong><code>datetime<\/code><\/strong><\/td><td>It deals with date and time object combinations.<\/td><\/tr><tr><td><strong><code>timedelta<\/code><\/strong><\/td><td>This class provides functionality related to intervals of time. Used in calculations of past and future date and time objects.<\/td><\/tr><tr><td><strong><code>info<\/code><\/strong><\/td><td>It deals with time zone info of local time.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We are going to deal with these date and time classes in the example section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-to-use-datetime-module-in-python-programs\">How to Use Datetime Module in Python Programs<\/h3>\n\n\n\n<p>To create and modify new date and time objects, we need to import the module. We load them by using the following statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime<\/pre>\n\n\n\n<p>Or if you want only one of the modules-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date\n\nfrom datetime import time\n\nfrom datetime import datetime<\/pre>\n\n\n\n<p>We could create new objects that have different date and time values stored for manipulation using the Python datetime module.<\/p>\n\n\n\n<p>The following syntax would do the needful. It is just the pseudo-code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime\n\ndatetime.datetime(year_number, month_number, date_number, hours_number, minutes_number, seconds_number)<\/pre>\n\n\n\n<p>To format the dates into human-readable strings, we use the function <strong><code>strftime()<\/code><\/strong> that is available in the <code>datetime<\/code> module.<\/p>\n\n\n\n<p>The datetime module has another function <strong><code>timedelta()<\/code><\/strong>\u00a0. It allows calculations on the date and time objects and is useful for doing time operations.<\/p>\n\n\n\n<p>The syntax used in invoking <strong><code>timedelta()<\/code><\/strong> is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import timedelta\n\ntimedelta(days, hours, minutes, seconds, year,...)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-python-datetime-examples\">Python Datetime Examples<\/h3>\n\n\n\n<p>You can copy\/paste the below code either in a Python interpreter or in an online Python compiler and run it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-to-display-today-s-date-we-use-the-following-code\">To display today\u2019s date we use the following code:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime\n\nprint (datetime.date.today())<\/pre>\n\n\n\n<p><strong> Or <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date\n\nprint (date.today())<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-to-view-the-individual-date-components-we-need\">To view the individual date components, we need:<\/h4>\n\n\n\n<p>Python datetime provides the date class which keeps track of all date and time components. The below code illustrates how can we access them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date\n\nprint (date.today().day)\n\nprint (date.today().month)\n\nprint (date.today().year)\n\n#If you prefer to write in one sentence, write it as \n\nprint (date.today().day,date.today().month,date.today().year)<\/pre>\n\n\n\n<p>Since the above code is long, we can rewrite it as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date\n\nnow = date.today()\n\nprint (now.day)\n\nprint (now.month)\n\nprint (now.year)\n\n#You can also display the previous 3 sentences in one sentence \n\nprint (now.day, now.month, now.year)<\/pre>\n\n\n\n<p>This code is cleaner and compact. Try to write the code in the shortest way possible without affecting readability.<\/p>\n\n\n\n<p>The below picture shows the output of the code if you run it from the Python command line interface:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"797\" height=\"369\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Datetime-Example-1.jpeg\" alt=\"Python Datetime Example-1\" class=\"wp-image-7005\"\/><\/figure>\n<\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"h-find-out-the-weekday-of-the-current-date\">Find out the weekday of the current date:<\/h4>\n\n\n\n<p>You could also use the date class from Python datetime to find out the weekday of the current date:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date\n\nnow = date.today()\n\nprint (f\"The week of the day is {now.weekday()}\")<\/pre>\n\n\n\n<p>The following is the output of the above code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The week of the day is 4.<\/pre>\n\n\n\n<p>To format the date and time into human-readable strings, we use <strong><code>strftime()<\/code><\/strong> in the <code>datetime<\/code> module.<\/p>\n\n\n\n<p>The following example clarifies how to use this function and prints the date\/time in a styled manner:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime\nfrom datetime import datetime as tm\n\nToday = tm.now()\n\nprint (Today.strftime(\"%a, %B, %d, %y\"))<\/pre>\n\n\n\n<p>When you run it, you get the following output. Basically, all the date components print separately.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Fri, October, 13, 23<\/pre>\n\n\n\n<p>It is important to understand the format codes used in the above code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Format Code<\/th><th>Short Description<\/th><\/tr><\/thead><tbody><tr><td><code>%a<\/code><\/td><td>refers to the day<\/td><\/tr><tr><td><code>%B<\/code><br><\/td><td>refers to the month<\/td><\/tr><tr><td><code>%d<\/code><br><\/td><td>refers to the date<\/td><\/tr><tr><td><code>%y<\/code><\/td><td>refers to the year<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The above codes are the most common format <a href=\"https:\/\/techbeamers.com\/check-the-type-of-variables-in-python\/\" target=\"_blank\" rel=\"noopener\">types to use in Python<\/a> datetime programs.<\/p>\n\n\n\n<p>However, if you want to print the local date and time using the function <strong><code>strftime()<\/code><\/strong> like the way given in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime\nfrom datetime import datetime as tm\n\nToday = tm.now()\n\nprint (\"Today\u2019s date is \" ,Today.strftime(\"%c\"))<\/pre>\n\n\n\n<p>To display only the local date and local time separately, instead of<code>%c<\/code> use <code>%x<\/code>.<\/p>\n\n\n\n<p>The following is a sample Python Program using multiple date-time modules.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from datetime import date,time, datetime, timedelta\n\n# Time_gap is the timedelta object where\n# we can do different calculations on it.\n\nTime_gap = timedelta(hours = 23, minutes = 34)\n\nprint (\"Future time is\" , str(datetime.now() + Time_gap))<\/pre>\n\n\n\n<p>The output is as follows.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"223\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Datetime-Example-3.jpeg\" alt=\"Python Datetime Example-3\" class=\"wp-image-7007\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion-usages-of-datetime-module\"><a id=\"usages\"><\/a>Conclusion &#8211; Usages of Datetime Module<\/h2>\n\n\n\n<p>Python Datatime It is a powerful feature that allows us to manipulate the system&#8217;s date and time without risking any system changes. We can define a new date object and modify its representation.<\/p>\n\n\n\n<p>Arithmetic calculations can be done on the date and time objects for several purposes such as finding the future date, and the present year, formatting of date value into a string, creating a time management solution, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial explains how to work with the date and time values using Python datetime. This module provides classes for representing and manipulating dates and times. Please note that this module and its functions could come in quite handy for a variety of applications such as: -Display dates and times on web pages and perform [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":7008,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[92],"tags":[],"class_list":{"0":"post-7004","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python-programming-tutorials"},"jetpack_featured_media_url":"https:\/\/techbeamers.com\/wp-content\/uploads\/2019\/01\/Python-Datetime-Module-Functions-with-Examples.png","_links":{"self":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/7004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/comments?post=7004"}],"version-history":[{"count":1,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/7004\/revisions"}],"predecessor-version":[{"id":23494,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/7004\/revisions\/23494"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media\/7008"}],"wp:attachment":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media?parent=7004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/categories?post=7004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/tags?post=7004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}