{"id":17811,"date":"2017-07-12T16:15:57","date_gmt":"2017-07-12T13:15:57","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=17811"},"modified":"2018-01-09T09:35:47","modified_gmt":"2018-01-09T07:35:47","slug":"python-datetime-tutorial","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/","title":{"rendered":"Python Datetime Tutorial"},"content":{"rendered":"<p>Today we will learn how to work with dates and time. It&#8217;s an essential skill because in almost every Python program you work with time one way or another. It can be really confusing at first how to work with them because we have dates, time, timezones, timedeltas.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#intro\">1. Introduction about datetime module<\/a><\/dt>\n<dt><a href=\"#examples\">2. Examples<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#basic\">2.1. Basic Examples<\/a><\/dt>\n<\/dl>\n<dl>\n<dt><a href=\"#advanced\">2.2. Advanced Examples<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#tips\">3. Useful tips and cool tricks<\/a><\/dt>\n<dt><a href=\"#summary\">4. Summary<\/a><\/dt>\n<dt><a href=\"#homework\">5. Homework<\/a><\/dt>\n<dt><a href=\"#download\">6. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<p>[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<h2><a name=\"intro\"><\/a>1. Introduction about datetime module<\/h2>\n<p>Module <code>datetime<\/code> is based on 3 classes:<\/p>\n<ul>\n<li><code>date<\/code><\/li>\n<li><code>time<\/code><\/li>\n<li><code>datetime<\/code><\/li>\n<\/ul>\n<p>For example, let&#8217;s start with class <code>date<\/code>. It receieves 3 parameters: year, month and day. Let&#8217;s try to find out what day is today:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">from datetime import date\r\ntoday = date.today()\r\nprint(today)<\/pre>\n<p>It prints the day in such format: <code>year<\/code> &#8211; <code>month<\/code> &#8211; <code>day<\/code>. For instance, we want to print it like this: <code>day<\/code>.<code>month<\/code>.<code>year<\/code>. What should we do? We can use <code>format()<\/code> method.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">from datetime import date\r\ntoday = date.today()\r\nprint(\"{}.{}.{}\".format(today.day, today.month, today.year))<\/pre>\n<p>Pretty obvious and easy, right?<br \/>\nLet&#8217;s take a quick look at class <code>time<\/code>. It receives parameters like hour, minute, second, microsecond. All these are not mandatory, so if we skip some, they will be initialized as zero.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">from datetime import datetime\r\ndatetime.now().time()<\/pre>\n<p>The output isn&#8217;t really pretty. What if we want to have a pretty output with the date and time alltogether but only using <code>time<\/code> module? Is it possible? For Python <span style=\"text-decoration: line-through;\">almost<\/span> everything is possible.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import time\r\ntime.ctime()<\/pre>\n<p>Sounds reasonable, right?<br \/>\nThe last one is class <code>datetime<\/code>. It expands previous examples by working together with time and date. It receives all parameters from previous two, but the first 3 are a must to complete. Let&#8217;s create the variable <code>now<\/code> that stores information about date and time. Then we will print only date and only time using <code>format()<\/code> method.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">from datetime import datetime\r\nnow = datetime.now()\r\nprint(now)\r\nprint(\"{}.{}.{} {}:{}\".format(now.day, now.month, now.year, now.hour, now.minute))\r\nprint(now.date())\r\nprint(now.time())<\/pre>\n<p>Check it out for yourself. The last thing I want to mention before we go any further. There is a method called <code>strptime()<\/code> which is used to convert string to datetime variable. There are couply things that may look like Regular Expressions you should know:<\/p>\n<ul>\n<li><code>%d<\/code> day of the month;<\/li>\n<li><code>%m<\/code> number of the month;<\/li>\n<li><code>%y<\/code> year with 2 digits;<\/li>\n<li><code>%Y<\/code> year with 4 digits;<\/li>\n<li><code>%H<\/code> hour in 24 format;<\/li>\n<li><code>%M<\/code> minute;<\/li>\n<li><code>%S<\/code> second;<\/li>\n<\/ul>\n<h2><a name=\"examples\"><\/a>2. Examples<\/h2>\n<h3><a name=\"basic\"><\/a>2.1 Basic examples<\/h3>\n<p>Let&#8217;s start off with some basic examples. First, we want to store a data about the date and print it out. By the time you are reading this, you should already know how to do it. If you don&#8217;t know, don&#8217;t worry. We will simply create a variable that will store a data about the date we want.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python;highlight:[1,2]; wrap-lines:false\">import datetime\r\nday = datetime.date(2017,7,12) # be careful to NOT put 07. It will give you a syntax error\r\nprint(day) #2017-07-12<\/pre>\n<p>Now, if we want to print out a current day, month and year seperately, we can simply do the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nday = datetime.date.today() \r\nprint(day.year) #outputs the current year\r\nprint(day.month) #outputs the current month\r\nprint(day.day) #outputs the current day<\/pre>\n<p>Another interesting thing is we can actually know which day of the week it is by using methods <code>weekday()<\/code> and <code>isoweekday()<\/code>. Let&#8217;s try it out:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nday = datetime.date.today() \r\nprint(day.weekday()) #outputs 0\r\nprint(day.isoweekday()) #outputs 1<\/pre>\n<p>The only difference between those 2 methods is if today is Monday (this is my case, becase I am writing this on Monday), <code>weekday()<\/code>&gt; returns <code>0<\/code> for Monday and <code>6<\/code> for Sunday. The <code>isoweekday()<\/code> returns <code>1<\/code> for Monday and <code>7<\/code> for Sunday. It doesn&#8217;t make much difference as you can see.<br \/>\nThere is another cool feature called <code>timedelta<\/code>. It is basically the difference between 2 days. For example, we want to know what day will be in 15 days from today. What should we do?<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nnow = datetime.date.today()\r\ndelta = datetime.timedelta(days=15)\r\nprint(now + delta)<\/pre>\n<p>Well, you might be wondering why this is important. Good thinking, let&#8217;s find out how many days are left before my birthday, for example:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nnow = datetime.date.today()\r\nmy_birthday = datetime.date(2017, 9 ,20) #we put 2017 as a year because we want to find out how many days are left within this year\r\ndays_left = my_birthday - now\r\nprint(days_left.days)<\/pre>\n<p>We have basically covered most of the <code>datime<\/code> module. However, there is one thing left. We didn&#8217;t really work with Timezones, right? I recommend you to go along with me right now and go install <code>pytz<\/code> package from <code>pip<\/code> by simply typing <code>pip install pytz<\/code> in your terminal. Then we are ready to start:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport pytz\r\ndate_timezone = datetime.datetime(2017, 7, 12, 10, 30, 45, tzinfo=pytz.UTC)\r\nprint(date_timezone)\r\n<\/pre>\n<p>Let&#8217;s find out how to print the current date and time including UTC using methods <code>now()<\/code> and <code>utcnow()<\/code>.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport pytz\r\nnow_timezone = datetime.datetime.now(tz=pytz.UTC)\r\nprint(now_timezone)\r\nutcnow_timezone = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)\r\nprint(utcnow_timezone)\r\n<\/pre>\n<p>If you follow along, you may find that both outputs are the same. So it doesn&#8217;t really matter which one to use. It&#8217;s all up to you. Now let&#8217;s go and see what happens if we put specific timezone.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport pytz\r\nutcnow_data = datetime.datetime.now(tz=pytz.UTC)\r\nprint(utcnow_data)\r\nmy_data = utcnow_data.astimezone(pytz.timezone(\"US\/Eastern\"))\r\nprint(my_data)\r\n<\/pre>\n<p>As you can see by its output, we see the difference between <code>utcnow_data<\/code> and <code>my_data<\/code>. So you are probably wondering, &#8216;How do I know which timezone to put there?&#8217;. That&#8217;s a very good question. Except Google, we can surely type in all timezones so that we won&#8217;t have to read official documentation to <code>pytz<\/code>. Note: I encourage you to read the official documentation of <code>pytz<\/code>.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport pytz\r\nfor tz in pytz.all_timezones:\r\n   print(tz)\r\n<\/pre>\n<p>So you can choose any you want from that list. For example, we want to have a pretty formated date with an American version where <code>month<\/code> goes first, then we have <code>date<\/code> and finally <code>year<\/code>. What should we do? If you are thinking about the method called <code>strftime()<\/code>, then you are thinking in the right direction! What it does is to transform the data from naive to readable.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport pytz\r\ndt_eastern = datetime.datetime.now(tz=pytz.timezone(\"US\/Eastern\"))\r\nprint(dt_eastern.strftime(\"%B %d %Y\"))\r\n<\/pre>\n<p>Now, you don&#8217;t have to memorize this. If you ever feel like printing something in specific format, just go to official site and see all attributes or go to a different chapter in this article where I put all attributes, so you won&#8217;t have to google so much.<br \/>\nIn other case, if we want to convert the string into a date, we can use a method called <code>strptime<\/code>. For instance, we have a date called <code>July 12, 2017<\/code> and we want to convert it to a readable but structured data. Let&#8217;s do the following:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nour_string = \"July 12, 2017\"\r\nresult = datetime.datetime.strptime(our_string, \"%B %d %Y\")\r\nprint(result)\r\n<\/pre>\n<h3><a name=\"advanced\"><\/a>2.2 Advanced examples<\/h3>\n<p>Let&#8217;s think about some programs that may require working with <code>datetime<\/code> module. For example, we want to understand what happened in specific date. How can we do it? Surely, we can google a lot, but <code>Wikipedia<\/code> may seem like a better answer since it has an open <code>API<\/code>. Let think what we can do:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Wikipedia.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\n\r\nanswer_format = \"%m\/%d\" # as for Month and Day\r\nlink_format = \"%b_%d\"\r\nlink = \"https:\/\/en.wikipedia.org.wiki\/{}\"\r\n\r\nwhile True:\r\n\r\n   answer = input(\"\"\"\r\n      What date?\r\n      Use the MM\/DD format or QUIT\"\"\")\r\n\r\n   if answer.upper() == \"QUIT\": #in case if user inputs quit\r\n\r\n      break #the program stops\r\n\r\n   try:\r\n\r\n      date = datetime.datetime.strptime(answer, answer_format)\r\n      result = link.format(date.strftime(link_format))\r\n      print(result)\r\n\r\n   except  ValueError:\r\n\r\n      print(\"Not a valid date\") #handling exception \r\n<\/pre>\n<p>Well, that went well, didn&#8217;t it? Let us think about some other examples. For example, we have to work with big data or we just need to generate random data to work with it. How can we do that? Let&#8217;s say, I want to generate data for year, month, day and birthday. It doesn&#8217;t need to be specific, we just need a lot of this data.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>random.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">from datetime import datetime\r\nimport random #in case we want a random data\r\n\r\nyear = random.randint(1917, 2017)\r\nmonth = random.randint(1, 12)\r\nday = random.randint(1, 28) #don't forget about February\r\n\r\nbirth_date = datetime(year, month, day)\r\n<\/pre>\n<p>That&#8217;s it! We can print this data and generate loop, so it iterates thus generates random data as much as we want!<br \/>\nOkay, this wasn&#8217;t too bad, right? Now, what if we want to know the current OS time and maybe have a timer? How can we do that?<\/p>\n<p><span style=\"text-decoration: underline;\"><em>timer.py<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nimport sys\r\n\r\nif __name__ == \"__main__\":\r\n\r\n   while True:\r\n      do = input(\"Enter:\\n\\\"T\\\" to use the timer, \\\"C\\\" to check the current time and \\\"Q\\\" to exit: \")\r\n      if do.upper() == \"T\":\r\n\r\n         while True:\r\n\r\n            start = input(\"Enter any KEY to start:\")\r\n            if start:\r\n               break\r\n\r\n         startTime = datetime.datetime.now()\r\n         print(\"You Started at \", startTime.hour, \":\", startTime.minute, \":\", startTime.second, sep = \"\")\r\n\r\n         while True:\r\n\r\n            end = input(\"Enter any KEY to stop:\")\r\n            if end:\r\n               break\r\n\r\n         endTime = datetime.datetime.now()\r\n         print(\"You ended at \", endTime.hour, \":\", endTime.minute, \":\", endTime.second, sep = \"\")\r\n         print(\"You took\", endTime.hour - startTime.hour, \"Hour(s),\", endTime.minute - startTime.minute, \"Minute(s),\", endTime.second - startTime.second, \"Second(s)\")\r\n\r\n      elif do.upper() == \"Q\":\r\n         sys.exit()\r\n\r\n      elif do.upper() == \"C\":\r\n         hms = datetime.datetime.now()\r\n\r\n         if hms:\r\n               break\r\n         hms = datetime.datetime.now()\r\n         print(hms.hour, \":\", hms.minute, \";\", hms.second, sep = \"\")\r\n\r\n      else:\r\n         print(\"That is not a valid command\")\r\n<\/pre>\n<h2><a name=\"tips\"><\/a>3. Useful tips and cool tricks<\/h2>\n<p>There are many cool things you can do with <code>datetime<\/code> module. Let&#8217;s cover some of them. How can we access the current month name with a full month name? Well, that&#8217;s pretty easy&#8230;<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nprint(datetime.date.today().strftime(\"%B\"))\r\n<\/pre>\n<p>Well, this one was easy. What if we want to have a current date in day name and month name? Sounds like something similar? Well, it is!<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import datetime\r\nprint(datetime.date.today().strftime(\"%A, %d %B, %Y\"))\r\n<\/pre>\n<p>Now, let&#8217;s think we want to have a program that works and runs just fine but with some pause in between? What should we do? Well, that is quite easy actually, all thanks to <code>time<\/code> module.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python<\/em><\/span><\/p>\n<pre class=\"brush:python\">import time\r\nfor i in range(10):\r\n   time.sleep(5)\r\n   print(\"Slept for 5 seconds\")\r\n<\/pre>\n<p>Basically we are getting the program that prints 10 times with an interval of 5 seconds the string <code>\"Slept for 5 seconds\"<\/code>. It&#8217;s pretty easy. Now, what we need here is some info showing what <code>strftime()<\/code> and <code>strptime()<\/code> do:<\/p>\n<ul>\n<li><code>%a<\/code> weekday as locale&#8217;s abbreviated name<\/li>\n<li><code>%A<\/code> weekday as locale&#8217;s full name<\/li>\n<li><code>%w<\/code> weekday as a decimal number, where 0 is Sunday and 6 is Saturday<\/li>\n<li><code>%d<\/code> day of the month as a zero-padded decimal number&gt;<\/li>\n<li><code>%b<\/code> month as locale&#8217;s abbreviated name<\/li>\n<li><code>%B<\/code> month as locale&#8217;s full name<\/li>\n<li><code>%m<\/code> month as a zero-padded decimal number<\/li>\n<li><code>%y<\/code> year without century as a zero-padded decimal number<\/li>\n<li><code>%Y<\/code> year without century as a decimal number<\/li>\n<li><code>%H<\/code> hour(24) as a zero-padded decimal number<\/li>\n<li><code>%I<\/code> hour(12) as a zero-padded decimal number<\/li>\n<li><code>%p<\/code> equivalent of either AM or PM<\/li>\n<li><code>%M<\/code> minute as a zero-padded number<\/li>\n<li><code>%S<\/code> second as a zero-padded number<\/li>\n<li><code>%z<\/code> UTC offset in the form ++HHMM or -HHMM<\/li>\n<li><code>%Z<\/code> time zone name<\/li>\n<li><code>%j<\/code> day of the year as a zero-padded decimal number<\/li>\n<li><code>%U<\/code> week number of the year<\/li>\n<li><code>%c<\/code> locale&#8217;s appropriate date and time presentation<\/li>\n<li><code>%X<\/code> another date and time representation<\/li>\n<\/ul>\n<h2><a name=\"summary\"><\/a>4. Summary<\/h2>\n<p>We have learnt how to do simple things with <code>datetime<\/code> module. Let&#8217;s understand basic principle of the entire module, shall we?<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Python shell<\/em><\/span><\/p>\n<pre class=\"brush:shell\">import datetime\r\nhelp(datetime)\r\n<\/pre>\n<p>We will see a lot of methods but those that we need are: <code>date<\/code>, <code>time<\/code>, and <code>datetime<\/code>. Each of them has a special structure:<\/p>\n<ol>\n<li><code>date(year, month, day)<\/code> &#8211; all parameters are essential and can&#8217;t be missing<\/li>\n<li><code>time([hour], [min], [sec], [microsec])<\/code> &#8211; if any are missing, they are initialized as <code>0<\/code><\/li>\n<li><code>datetime(year, month, day, [hour], [min], [sec], [microsec])<\/code> &#8211; only first 3 parametes are mandatory<\/li>\n<\/ol>\n<h2><a name=\"homework\"><\/a>5. Homework<\/h2>\n<p>There will be only one homework but with different levels of complexity. You are free to choose any level.<br \/>\nThe task is <i>to create a program that will tell you how many days are left before the &#8220;day&#8221; (special day like birthday or whatever)<\/i><br \/>\n<u>Easy level<\/u>: in the program, you already have programmed &#8220;day&#8221;, so there is no user interaction. The program works any day and returns different data.<br \/>\n<u>Medium level<\/u>: the program asks the user about the &#8220;day&#8221; (name, date, etc.). It returns different data depending on the day.<br \/>\n<u>Hard Level<\/u>: it has a solid GUI interface that has an input field for a user to put the data about the &#8220;day&#8221;. It returns different data depending on the day. P.S. you can use <code>Tkinter<\/code> or <code>Pygame<\/code> to do that.<br \/>\n<u>Guido level<\/u>: it has a solid GUI interface, and everytime you check how many days\/hours are left before the &#8220;day&#8221;, it puts the data about your actions in the program in a special file (for example, in .csv file). Moreover, it has a neural network that analyzes how many times and when you opened the app, so within some time it automatically opens it for you or\/and have some notifications to open the program.<\/p>\n<h2><a name=\"download\"><\/a>6. Download the Source Code<\/h2>\n<p>You can find 3 python programs that we discussed in advanced examples here.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here : <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2017\/07\/datetime.zip\"><strong>datetime.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we will learn how to work with dates and time. It&#8217;s an essential skill because in almost every Python program you work with time one way or another. It can be really confusing at first how to work with them because we have dates, time, timezones, timedeltas. Table Of Contents 1. Introduction about datetime &hellip;<\/p>\n","protected":false},"author":657,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[143,290],"class_list":["post-17811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-datetime","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Datetime Tutorial - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Today we will learn how to work with dates and time. It&#039;s an essential skill because in almost every Python program you work with time one way or another.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Datetime Tutorial - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Today we will learn how to work with dates and time. It&#039;s an essential skill because in almost every Python program you work with time one way or another.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-12T13:15:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:35:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aleksandr Krasnov\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aleksandr Krasnov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\"},\"author\":{\"name\":\"Aleksandr Krasnov\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\"},\"headline\":\"Python Datetime Tutorial\",\"datePublished\":\"2017-07-12T13:15:57+00:00\",\"dateModified\":\"2018-01-09T07:35:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\"},\"wordCount\":1638,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"keywords\":[\"datetime\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\",\"name\":\"Python Datetime Tutorial - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2017-07-12T13:15:57+00:00\",\"dateModified\":\"2018-01-09T07:35:47+00:00\",\"description\":\"Today we will learn how to work with dates and time. It's an essential skill because in almost every Python program you work with time one way or another.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Datetime Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373\",\"name\":\"Aleksandr Krasnov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g\",\"caption\":\"Aleksandr Krasnov\"},\"description\":\"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Datetime Tutorial - Web Code Geeks - 2026","description":"Today we will learn how to work with dates and time. It's an essential skill because in almost every Python program you work with time one way or another.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python Datetime Tutorial - Web Code Geeks - 2026","og_description":"Today we will learn how to work with dates and time. It's an essential skill because in almost every Python program you work with time one way or another.","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2017-07-12T13:15:57+00:00","article_modified_time":"2018-01-09T07:35:47+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Aleksandr Krasnov","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Aleksandr Krasnov","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/"},"author":{"name":"Aleksandr Krasnov","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373"},"headline":"Python Datetime Tutorial","datePublished":"2017-07-12T13:15:57+00:00","dateModified":"2018-01-09T07:35:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/"},"wordCount":1638,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","keywords":["datetime","python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/","name":"Python Datetime Tutorial - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2017-07-12T13:15:57+00:00","dateModified":"2018-01-09T07:35:47+00:00","description":"Today we will learn how to work with dates and time. It's an essential skill because in almost every Python program you work with time one way or another.","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-datetime-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Datetime Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/e07867bd46d3c03c70d9566277772373","name":"Aleksandr Krasnov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/773b350dfd4fa0aa4470113fe22f39c38db44fc456aef63c609bf8f05e424dc6?s=96&d=mm&r=g","caption":"Aleksandr Krasnov"},"description":"Aleksandr is passionate about teaching programming. His main interests are Neural Networks, Python and Web development. Hobbies are game development and translating. For the past year, he has been involved in different international projects as SEO and IT architect.","url":"https:\/\/www.webcodegeeks.com\/author\/aleksandr-krasnov\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/17811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/657"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=17811"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/17811\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=17811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=17811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=17811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}