{"id":7385,"date":"2020-07-28T10:45:49","date_gmt":"2020-07-28T10:45:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7385"},"modified":"2020-08-07T12:50:39","modified_gmt":"2020-08-07T12:50:39","slug":"python-date-class","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-date-class","title":{"rendered":"Python date Class &#8211; 7 functions to know!"},"content":{"rendered":"\n<p>Hey! In this article, we will be focusing on Python date class in detail. So, let us get started.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is Python datetime module?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/python-datetime-module\" class=\"rank-math-link\">Python datetime module<\/a> offers us with various classes and functions to deal with and manipulate the data values in terms of date and time values.<\/p>\n\n\n\n<p>We need to import the datetime module in order to access the different classes and methods contained in it.<\/p>\n\n\n\n<p>Here, you can use the below snippet of code to import the date class and create a date object through user-defined parameters&#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import date\n \ndte = date(2020,12,27)\n \nprint(&quot;Date: &quot;, dte)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nDate:  2020-12-27\n<\/pre><\/div>\n\n\n<p>Now, let us understand the date class of Python datetime module in the upcoming section.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing methods of date class<\/h2>\n\n\n\n<p>Python date class contains various methods to work with date format of the data values.<\/p>\n\n\n\n<p>By using the functions of the date class, we can easily manipulate and frame the data values into the standard date format <strong>YYYY-MM-DD<\/strong>.<\/p>\n\n\n\n<p>Let us now have a look at some of the important functions framed by the date class function in detail.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. date.today() function<\/h3>\n\n\n\n<p>The <code>date.today() function<\/code> fetches the current date from the system and represents the same.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.today()\n<\/pre><\/div>\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 date\n \ndte = date.today()\n \nprint(&quot;Date: &quot;, dte)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nDate:  2020-07-25\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. date.year() function<\/h3>\n\n\n\n<p>We can access and fetch the year from the date expression using year function. The <code>year function<\/code> extracts and represents the year value from the provided date expression.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.today().year\n<\/pre><\/div>\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,date\n \nyear = date.today().year\nprint(year)\n\n<\/pre><\/div>\n\n\n<p>From date.today() function, we get the current date i.e 2020-07-26. Further to which, we have extracted the year value using year() function.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n2020\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. date.month() function<\/h3>\n\n\n\n<p>In order to extract and represent the month value, <code>month function <\/code>can be used.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.today().month\n<\/pre><\/div>\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,date\n \nmnth = date.today().month\nprint(mnth)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n7\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. date.day() function<\/h3>\n\n\n\n<p>The day value from a date expression can be easily extracted using<code> day function<\/code> as shown below&#8211;<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.day\n<\/pre><\/div>\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,date\n \nday = date.today().day\nprint(day)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong> 25<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. date.replace() function<\/h3>\n\n\n\n<p>At times, a situation may arise, when we would want to alter the date portions of the date expression. This task can be achieved using replace() function. <\/p>\n\n\n\n<p>The <code>date.replace() function<\/code> can be used to replace the below date portions&#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>year<\/li><li>month<\/li><li>day<\/li><\/ul>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.replace(year,day,month)\n<\/pre><\/div>\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,date\n \ndt = date.today()\nprint(&quot;Current date: &quot;,dt)\n\nres = dt.replace(year=2021,day=20)\nprint(&quot;Modified date: &quot;,res)\n\n<\/pre><\/div>\n\n\n<p>In the above example, we have replaced the year and day values from the current date (2020-07-26).<\/p>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nCurrent date:  2020-07-26\nModified date:  2021-07-20\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. date.weekday() function<\/h3>\n\n\n\n<p>Using the <code>weekday function<\/code>, we can fetch the weekday number of the day value from the date expression.<\/p>\n\n\n\n<p>The weekdays are provided the index as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Monday-0<\/li><li>Tuesday-1<\/li><li>Wednesday-2<\/li><li>Thursday-3<\/li><li>Friday-4<\/li><li>Saturday-5<\/li><li>Sunday-6<\/li><\/ul>\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,date\n \ndate = date.today().weekday()\nprint(&quot;Weekday: &quot;,date)\n\n<\/pre><\/div>\n\n\n<p>In the above example, we have calculated the weekday number for the current date: 2020-07-26.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n6\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. date.strftime() function<\/h3>\n\n\n\n<p>The <code>date.strftime() function<\/code> enables us to extract the date portions of the date expressions and represent the values as a String.<\/p>\n\n\n\n<p>To understand the variants of strftime() function, do visit <a href=\"https:\/\/www.askpython.com\/python-modules\/python-strftime\" class=\"rank-math-link\">Python strftime() function<\/a>. <\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndate.strftime(&quot;%Y-%m-%d&quot;)\n<\/pre><\/div>\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,date\n\ndate = date.today() \n\nyear = date.strftime(&quot;%Y&quot;)\nprint(&quot;Current Year:&quot;, year)\n\nstr_date = date.strftime(&quot;%Y-%m-%d&quot;)\nprint(&quot;Date value:&quot;,str_date)\t\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nCurrent Year: 2020\nDate value: 2020-07-26\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below in case you come across any doubt.<\/p>\n\n\n\n<p>Till then, Happy Learning!!<\/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><a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html#date-objects\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python date class &#8212; Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey! In this article, we will be focusing on Python date class in detail. So, let us get started. What is Python datetime module? Python datetime module offers us with various classes and functions to deal with and manipulate the data values in terms of date and time values. We need to import the datetime [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":7440,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7385","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=7385"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7385\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7440"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}