{"id":3834,"date":"2020-06-25T17:30:27","date_gmt":"2020-06-25T12:00:27","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3834"},"modified":"2020-06-25T17:30:29","modified_gmt":"2020-06-25T12:00:29","slug":"python-datetime-timedelta-strftimeformat-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-datetime-timedelta-strftimeformat-with-examples\/","title":{"rendered":"Python DateTime, TimeDelta, Strftime(Format) with Examples"},"content":{"rendered":"\n<p>While working with real-time applications, you have a lot to do with date and time. For example, an inventory management system you have to keep track of when a specific product was received and sold. You have to print statements at specific intervals. In all these tasks data and time are used.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\">Python <\/a>provides a \u2018DateTime\u2019 class under which 5 major classes are defined.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u2018date\u2019<\/strong> \u2013 used to get just date ( Month, day, year)<\/li><li><strong>\u2018time\u2019<\/strong> \u2013 Time independent of the day (Hour, minute, second, microsecond)<\/li><li><strong>\u2018datetime\u2019<\/strong> \u2013 Both time and date (Month, day, year, hour, second, microsecond)<\/li><li><strong>\u2018timedelta\u2019<\/strong>\u2014 A duration of time used for manipulating dates<\/li><li><strong>\u2018tzinfo\u2019<\/strong>\u2014 An abstract class for dealing with time zones<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">DateTime<\/h2>\n\n\n\n<p>Let\u2019s take a look at how to get data and time. For using data and time we need to import the following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>From datetime import date\nprint(date.today())<\/code><\/pre>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" width=\"624\" height=\"192\" src=\"https:\/\/lh4.googleusercontent.com\/tigEF6-ijDjSII-2SoYrGNSCiwfSZhDeJWv1mGEID98swQ-ifDEIrJNAxGe-RaoSjtrX6j7LbsTrIdDqaR8l-UeAQU3duggsHVjaxf8zPqQWZ6CmBqZZlAVOdMv0k8XagZuC5pXUaRIULMRspw\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>We can access the year, month and the day separately. Let\u2019s have a look at the example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import date\ntoday = date.today()\nprint(\"day\",today.day)\nprint(\"Month\",today.month)\nprint(\"Year\",today.year)<\/code><\/pre>\n\n\n\n<p>The following will be the output.<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"624\" height=\"325\" src=\"https:\/\/lh3.googleusercontent.com\/fMnT_FaMAVHptM01wJ6S1v2h6ztARSqTp0FboFBjMvFljS2KT4Xo0-rtHl-LNGOEZ_whXLyr3_ziZVDEu0-c-z3lMgJkZfEKpS0ejiS5Y4UiVcpVfJm-iBTJGcbw6v6ya6UrBm9wO-O2aoUv-Q\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>For the current date and time will use \u201cdatetime.now()\u201d function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime\nprint(datetime.now())<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" width=\"537\" height=\"166\" src=\"https:\/\/lh3.googleusercontent.com\/xdO7lE9i74ma5f7WW3r1z4bu5WTSlVxA_PDDVHN0NSgUXhh8GmaOJD-iM3JVldtYozo-Yspu88nKTITUqVCm9AIxjPu1Dy7ophZaKYbFNMKBpRJbACJHc7jKfJDBk5qOPYSC-e71-ESR6AlLNw\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TimeDelta<\/h2>\n\n\n\n<p>Python <strong>timedelta()<\/strong> function is present under<em> DateTime library<\/em> which is generally used for calculating differences in dates and also can be used for date manipulations in Python.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import timedelta\nprint (timedelta(days=200, hours=4, minutes=5))<\/code><\/pre>\n\n\n\n<p>The output of the above code is shown below.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"159\" src=\"https:\/\/lh4.googleusercontent.com\/yaTw_4R-cMkJa_FtG-FRcVU085Rk9HnuvUExPLhuM8H56Ruf55O5e5ULnynrahGU4ILjZUJ9FwPEcaJloxH1lYjWTd1CyTvslrzcCRF3nyzwFC4MLnHz7gPwh-bpwmjpAbmA7VEbwPxELBVsIA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Let\u2019s take a look at some practical example. Let\u2019s print today date one year from now.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"187\" src=\"https:\/\/lh3.googleusercontent.com\/jzBvk75i3zhIRyKY5fGJBhTzfjsVory9I4-UQl_ADqcQcL7ohXuLbU0Le5oOUV_dPAOLzYE5eTnUIb5vC3ZF49RaZt16hoQx-MOguvpg4q3-W7APTs1b0gZy7q2sZ-iSl3Y1ULWn073FXWJ2gw\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Strftime(Format)<\/h2>\n\n\n\n<p>The strftime() method is used to get a string representing date and time using date,<a href=\"https:\/\/www.programiz.com\/python-programming\/datetime#time\" rel=\"nofollow noopener\" target=\"_blank\"> <\/a>time or<a href=\"https:\/\/www.programiz.com\/python-programming\/datetime#datetime\" rel=\"nofollow noopener\" target=\"_blank\"> <\/a>DateTime object.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>%C- indicates the local date and time<\/li><li>%x- indicates the local date<\/li><li>%X- indicates the local time<\/li><li>%Y- indicates the year<\/li><li>%m- indicates the month<\/li><li>%d- indicates the day<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime\n\nnow = datetime.now() # current date and time\n\nyear = now.strftime(\"%Y\")\nprint(\"year:\", year)\n\nmonth = now.strftime(\"%m\")\nprint(\"month:\", month)\n\nday = now.strftime(\"%d\")\nprint(\"day:\", day)\n\ntime = now.strftime(\"%H:%M:%S\")\nprint(\"time:\", time)\n\ndate_time = now.strftime(\"%m\/%d\/%Y, %H:%M:%S\")\nprint(\"date and time:\",date_time)<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"670\" src=\"https:\/\/lh5.googleusercontent.com\/xM13HSg9FqOiWZOKKmRptuWbuIoXH9t45nYP62COVGR6vZQm7sT7RscCunfBss1PZTQ-vC_tOB4C51KIDZaxt3WrsW1T11NtH-Tngq60aLiXzX4ZWqHa--Di1ZTrz-RKo81qO4zdUpxEmjF6Ew\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>The &#8220;strftime function&#8221; can also be used to print time in any format 24 hours or 12 hours.<\/p>\n\n\n\n<p>Let\u2019s take a look at an example.<\/p>\n\n\n\n<p><strong>12 hours<\/strong> time is declared [print now.strftime(&#8220;%I:%M\u201d) ]<\/p>\n\n\n\n<p><strong>24 hours <\/strong>time is declared [print now.strftime(&#8220;%H:%M&#8221;)]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime\nnow = datetime.now()\nprint(\"12 hours\",now.strftime(\"%I:%M\"))\nprint(\"24 hours\",now.strftime(\"%H:%M\"))<\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"273\" src=\"https:\/\/lh3.googleusercontent.com\/paDP7osGxz1J2VOA1jhp0A31Ie3VqarhsMOCNPC3zq1UV1k12t5U_H7x5GOq7T3-TMrEX-jHBMwAia0L3-6dvmHz7RhyxJJmvdfJdTvBM7klpMMT44yJmv_UFfwRWXQHYx36nD-v0bIeWnNqRg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working with real-time applications, you have a lot to do with date and time. For example, an inventory management system you have to keep track of when a specific product was received and sold. You have to print statements at specific intervals. In all these tasks data and time are used. Python provides a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3843,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1025,1024,1027,1026],"class_list":["post-3834","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-datetime","tag-python-datetime","tag-strftime","tag-timedelta"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3834"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3834\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3843"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}