{"id":226,"date":"2019-02-20T03:11:28","date_gmt":"2019-02-20T03:11:28","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=226"},"modified":"2026-07-04T01:21:27","modified_gmt":"2026-07-04T01:21:27","slug":"14-python-date-and-time-2","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/python\/14-python-date-and-time-2\/","title":{"rendered":"Python &#8211; Date and Time 2"},"content":{"rendered":"<p>In this lesson, you will learn how to work with Date, Time and DateTime classes in Python. Also we would learn how to format date and time data.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><a href=\"#t1\">Introduction to Python Date and Time<\/a><\/li>\n<li><a href=\"#t2\">Working with Current Date and Time<\/a><\/li>\n<li><a href=\"#t3\">Getting Day, Month, Year and Weekday<\/a><\/li>\n<li><a href=\"#t4\">Getting both Date and Time<\/a><\/li>\n<li><a href=\"#t5\">Working with only Time<\/a><\/li>\n<li><a href=\"#t6\">Formatting using<em> strftime()<\/em> function<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=cr5HBU-8N2Y&amp;feature=youtu.be\" target=\"_blank\" rel=\"noopener\">Watch the Video<\/a><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Introduction to Python Date and Time<\/strong><\/h4>\n<p>Python provides a number of module and several functions for working with date and time.<\/p>\n<p>Before you can work with date, time and datetime codes, you need to import the necessary modules. These modules are date, time and datetime. This is shown below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">datetime<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> date\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">datetime<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> time\r\n<span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">datetime<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> datetime\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Working with Current Date and Time<\/strong><\/h4>\n<p>The first thing you want to do with date and time is to get today&#8217;s date. To do that you use the today() function in the date module.<\/p>\n<p>The code below prints out the today&#8217;s date<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">today <span style=\"color: #333333;\">=<\/span> date<span style=\"color: #333333;\">.<\/span>today()\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Today's date is \"<\/span>, today)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you run the above code, you will have the output below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Today<span style=\"background-color: #fff0f0;\">'s date is  2019-02-20<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Getting Day, Month, Year and Weekday<\/strong><\/h4>\n<p>You will notice that the date is made up of day, month and year. We can therefore separate a date in to the three components. This is done using the day, month and year methods.<\/p>\n<p>The code below prints out the day, month and year in today&#8217;s date.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">today <span style=\"color: #333333;\">=<\/span> date<span style=\"color: #333333;\">.<\/span>today()\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Today's date is \"<\/span>, today)\r\n\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Day: \"<\/span>, today<span style=\"color: #333333;\">.<\/span>day)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Month: \"<\/span>, today<span style=\"color: #333333;\">.<\/span>month)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Year: \"<\/span>, today<span style=\"color: #333333;\">.<\/span>year)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Run this code and examine the output<\/p>\n<p>You can also get the weekday using the <strong><em>today.weekday()<\/em><\/strong> function. If you use this function, you get the weekday&#8217;s number. This is such that Monday = 0, Tuesday = 1, Wednesday = 3, and so on. You can try this yourself.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Getting both Date and Time<\/strong><\/h4>\n<p>The two functions that gives you both date and time together are the <em>today()<\/em> and the <em>now()<\/em> functions. You use them to create datetime objects. These function however, are in the datetime module.<\/p>\n<p>The datetime object includes hours, minutes, seconds and microseconds.<\/p>\n<p>The code below prints a datetime object representing the current date and time.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">today <span style=\"color: #333333;\">=<\/span> datetime<span style=\"color: #333333;\">.<\/span>today()\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Today's date is \"<\/span>, today)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you execute this code, the output would be as shown below. You can also get the same output using the\u00a0<em>datetime.now()<\/em>\u00a0function.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Today<span style=\"background-color: #fff0f0;\">'s date is  2019-02-20 03:10:58.165653<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Working with Only Time<\/strong><\/h4>\n<p>To get only the time you need to use the time function. This function takes an argument of a datetime object from the datetime module.<\/p>\n<p>For example, the code below outputs only the current time<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Getting the current time<\/span>\r\ntoday <span style=\"color: #333333;\">=<\/span> datetime<span style=\"color: #333333;\">.<\/span>now()\r\ncurrenttime <span style=\"color: #333333;\">=<\/span> datetime<span style=\"color: #333333;\">.<\/span>time(datetime<span style=\"color: #333333;\">.<\/span>today())\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"The Current time is: \"<\/span>, currenttime)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The output of this code is the current time. Now we would write a code that separates the time into hours, minutes, seconds and microseconds. You can find the code below:<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Getting hours, minutes and seconds<\/span>\r\ntoday <span style=\"color: #333333;\">=<\/span> datetime<span style=\"color: #333333;\">.<\/span>now()\r\ncurrenttime <span style=\"color: #333333;\">=<\/span> datetime<span style=\"color: #333333;\">.<\/span>time(datetime<span style=\"color: #333333;\">.<\/span>today())\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"The Current time is: \"<\/span>, currenttime)\r\n\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Hours \"<\/span>, currenttime<span style=\"color: #333333;\">.<\/span>hour)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Minutes \"<\/span>, currenttime<span style=\"color: #333333;\">.<\/span>minute)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Seconds \"<\/span>, currenttime<span style=\"color: #333333;\">.<\/span>second)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Microseconds\"<\/span>, currenttime<span style=\"color: #333333;\">.<\/span>microsecond)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The output of the code is given below<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">The Current time <span style=\"color: #000000; font-weight: bold;\">is<\/span>:  <span style=\"color: #0000dd; font-weight: bold;\">03<\/span>:<span style=\"color: #0000dd; font-weight: bold;\">36<\/span>:<span style=\"color: #6600ee; font-weight: bold;\">17.469424<\/span>\r\nHours  <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>\r\nMinutes  <span style=\"color: #0000dd; font-weight: bold;\">36<\/span>\r\nSeconds  <span style=\"color: #0000dd; font-weight: bold;\">17<\/span>\r\nMicroseconds <span style=\"color: #0000dd; font-weight: bold;\">469424<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Formatting Date and Time using strftime()<\/strong><\/h4>\n<p>The strftime() is a method that applies to both date and time. It take various format parameters to format the date\/time objects.<\/p>\n<p>For example, the code below displays the current date and time in 24hour format. It also displays the weekday.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007020;\">print<\/span>(datetime<span style=\"color: #333333;\">.<\/span>now()<span style=\"color: #333333;\">.<\/span>strftime(<span style=\"background-color: #fff0f0;\">\"%A %b %d %Y by %I:%M %p\"<\/span>))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Find a <a href=\"http:\/\/strftime.org\/\" target=\"_blank\" rel=\"noopener\">complete list of strftime() function formats here.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/cr5HBU-8N2Y\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<p><!-- ktg-lesson-nav --><\/p>\n<nav class=\"ktg-lesson-nav\" aria-label=\"Lesson navigation\">\n<p><strong>Previous:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/21-python-map-filter-reduce\/\">Lesson 20: Python &#8211; Map, Filter, Reduce<\/a><\/p>\n<p><strong>Next:<\/strong> <a href=\"https:\/\/kindsonthegenius.com\/python\/python-object-oriented-programming\/\">Lesson 22: Python &#8211; Object Oriented Programming<\/a><\/p>\n<\/nav>\n<p><!-- ktg-alkademy-cta --><\/p>\n<div class=\"ktg-alkademy-cta\" style=\"margin:2em 0;padding:1.25em;border-left:4px solid #2563eb;background:#f8fafc;\">\n<p><strong>Want live Python classes?<\/strong> Join <a href=\"https:\/\/www.alkademy.com\/courses\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a> for instructor-led Python and data science courses with hands-on projects.<\/p>\n<\/div>\n<p><!-- ktg-author-trust --><\/p>\n<div class=\"ktg-author-trust\" style=\"margin:2em 0;padding:1.25em;border-top:1px solid #e2e8f0;\">\n<p>Kindson Munonye is a software engineer and technical author specializing in Python, data science, and machine learning. He publishes free step-by-step Python tutorials and live classes on Alkademy.<\/p>\n<p><a href=\"https:\/\/github.com\/KindsonTheGenius\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> \u00b7 <a href=\"https:\/\/www.linkedin.com\/in\/kindson\" target=\"_blank\" rel=\"noopener noreferrer\">LinkedIn<\/a> \u00b7 <a href=\"https:\/\/www.kindsonthegenius.com\/about\/\">About<\/a> \u00b7 <a href=\"https:\/\/www.alkademy.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alkademy<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, you will learn how to work with Date, Time and DateTime classes in Python. Also we would learn how to format date &hellip; <\/p>\n","protected":false},"author":395,"featured_media":227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[5],"tags":[12,20],"class_list":["post-226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-python-date-and-time","tag-strftime"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":6,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/226\/revisions\/587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}