{"id":2080,"date":"2019-04-13T08:49:56","date_gmt":"2019-04-13T08:49:56","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=2080"},"modified":"2022-03-08T17:05:08","modified_gmt":"2022-03-08T17:05:08","slug":"datetime-python-examples","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/python\/datetime-python-examples\/","title":{"rendered":"datetime in Python &#8211; Simplified Guide with Clear Examples"},"content":{"rendered":"<p><em><code>datetime<\/code> is the standard module for working with dates in python. It provides 4 main objects for date and time operations: <code>datetime<\/code>, <code>date<\/code>, <code>time<\/code> and <code>timedelta<\/code>. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.<\/em><\/p>\r\n<p><!-- \/wp:post-content --> <img decoding=\"async\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2019\/04\/Python_datetime-min.jpg\" \/>datetime in Python &#8211; Simplified Guide with Clear Examples. Photo by Sergio.<\/p>\r\n<h2 id=\"content\">Content<\/h2>\r\n<ol>\r\n<li>Introduction to datetime<\/li>\r\n<li>How to get the current date and the time in Python<\/li>\r\n<li>How to create the datetime object<\/li>\r\n<li>How to parse a string to datetime in python?<\/li>\r\n<li>How to format the datetime object into any date format?<\/li>\r\n<li>Useful datetime functions<\/li>\r\n<li>When and how to use the datetime.time() class?<\/li>\r\n<li>When and how to use the datetime.timedelta() class?<\/li>\r\n<li>Timezones<\/li>\r\n<li>14 Practice Exercises with Solutions<\/li>\r\n<\/ol>\r\n<h2 id=\"1introductiontodatetime\">1. Introduction to datetime<\/h2>\r\n<p>The <code>datetime<\/code> is the main module for working with dates in python. Whenever you need to work with dates in python, <code>datetime<\/code> module provides the necessary tools. <code>datetime<\/code> is part of python&#8217;s standard library, which means, you don&#8217;t need to install it separately. You can simply import as is.<\/p>\r\n<pre><code class=\"python language-python\">import datetime\r\n<\/code><\/pre>\r\n<p>If you have to learn only one thing about handling dates in <code>datetime<\/code> module, it is the <code>datetime.datetime()<\/code> class. Inside <code>datetime<\/code> module, the most important and the commonly used object is the <code>datetime<\/code> class. Notice, I am talking about the <code>datetime<\/code> class inside the <code>datetime<\/code> module. Since both the module and the class have the same name, pay attention to what object you are using. Alright, besides the <code>datetime.datetime<\/code> class, there is also the:<\/p>\r\n<ul>\r\n<li><code>date<\/code> class<\/li>\r\n<li><code>time<\/code> class<\/li>\r\n<li><code>timedelta<\/code> class<\/li>\r\n<\/ul>\r\n<p>Each these classes have its own purpose. We&#8217;ll cover all of these in this post and about a more advanced parser (not in `datetime`) to help parse any date.<\/p>\r\n<p><!-- wp:paragraph \/--><!-- \/wp:paragraph -->\r\n\r\n<!-- wp:paragraph --><\/p>\r\n<h2 id=\"2howtogetthecurrentdateandthetime\">2. How to get the current date and the time in Python<\/h2>\r\n<p>The <code>datetime.datetime.now()<\/code> method gives the current datetime.<\/p>\r\n<pre><code class=\"python language-python\">datetime.datetime.now()\r\n#&gt; datetime.datetime(2019, 2, 15, 18, 54, 58, 291224)\r\n<\/code><\/pre>\r\n<p>The output is a nice <code>datetime.datetime<\/code> object with the current date and time in local time zone. The output is in the following order: <strong>&#8216;year&#8217;, &#8216;month&#8217;, &#8216;date&#8217;, &#8216;hour&#8217;, &#8216;minute&#8217;, &#8216;seconds&#8217;, &#8216;microseconds&#8217;<\/strong>. To get the date alone, use the <code>datetime.date.today()<\/code> instead.<\/p>\r\n<pre><code class=\"python language-python\">datetime.date.today()\r\n#&gt; datetime.date(2019, 2, 15)\r\n<\/code><\/pre>\r\n<p>It returns a <code>datetime.date<\/code> object and not <code>datetime.datetime<\/code>. Why? That&#8217;s because, <code>today()<\/code> is a method of the <code>datetime.date<\/code> class and does not contain time information. Good. But the above notation hard to read. Printing it out will show in a nice <code>YYYY-mm-dd<\/code> format.<\/p>\r\n<pre><code class=\"python language-python\">print(datetime.date.today())\r\n#&gt; 2019-02-15\r\n<\/code><\/pre>\r\n<p>We will see how to format datetime to many more formats shortly.<\/p>\r\n<h2 id=\"3howtocreatethedatetimeobject\">3. How to create the datetime object<\/h2>\r\n<p>We saw how to create the <code>datetime<\/code> object for current time. But how to create one for any given date and time? Say, for the following time: 2001-01-31::10:51:00 You can pass it in the same order to <code>datetime.datetime()<\/code>. (I will show an easier method in next section)<\/p>\r\n<pre><code class=\"python language-python\">datetime.datetime(2001, 1, 31, 10, 51, 0)\r\n#&gt; datetime.datetime(2001, 1, 31, 10, 51)\r\n<\/code><\/pre>\r\n<p>You can also create a <code>datetime<\/code> from a unixtimestamp. A unixtimestamp is nothing but the number of seconds since the epoch date: &#8216;Jan 01, 1970&#8217;<\/p>\r\n<pre><code class=\"python language-python\">mydatetime = datetime.datetime.fromtimestamp(528756281)\r\nmydatetime\r\n#&gt; datetime.datetime(1986, 10, 4, 2, 14, 41)\r\n<\/code><\/pre>\r\n<p>You can convert the <code>datetime<\/code> back to a unixtimestamp as follows:<\/p>\r\n<pre><code class=\"python language-python\">mydatetime.timestamp()\r\n#&gt; 528756281.0\r\n<\/code><\/pre>\r\n<h2 id=\"4howtoparseanydateformattodatetime\">4. How to parse a string to datetime in python?<\/h2>\r\n<p>The above method requires you to manually key in the year, month etc to create a datetime object. But, it not convenient when working with datasets or spreadsheet columns containing date strings. We need way to automatically parse a given date string, in whatever format, to a datetime object. Why is this needed? Because, datasets containing dates are often imported as strings. Secondly, the date can be in any arbitrary date string format, like, &#8216;2010 Jan 31&#8217; or &#8216;January 31, 2010&#8242; or even &#8217;31-01-2010&#8217;. So, How to convert a date string to a `datetime`? The <code>parser<\/code> module from <code>dateutil<\/code> let&#8217;s you parse pretty much any date string to a <code>datetime<\/code> object.<\/p>\r\n<pre><code class=\"python language-python\">from dateutil.parser import parse\r\nparse('January 31, 2010')\r\n#&gt; datetime.datetime(2010, 1, 31, 0, 0)\r\n<\/code><\/pre>\r\n<h4>\u00a0<\/h4>\r\n<p>&nbsp;<\/p>\r\n<h2 id=\"5example1parsingadatestringtodatetime\">5. Example 1 &#8211; Parsing a date string to datetime<\/h2>\r\n<p>Parse the following date string to a <code>datetime<\/code> object: &#8217;31, March 31, 2010, 10:51pm&#8217; <strong>Solution:<\/strong><\/p>\r\n<pre><code class=\"python language-python\">from dateutil.parser import parse\r\nparse('31, March 31, 2010, 10:51pm')\r\n<\/code><\/pre>\r\n<p>You can convert any datetime object to nearly any representation of date format using its <code>strftime()<\/code> method.<\/p>\r\n<h2 id=\"6howtoformatthedatetimeobjectintoanydateformat\">6. How to format the datetime object into any date format?<\/h2>\r\n<p>You can convert any datetime object to nearly any representation of date format using its <code>strftime()<\/code> method. You need to pass the right <a href=\"http:\/\/strftime.org\/\">symbol representaion of the date format<\/a> as an argument.<\/p>\r\n<pre><code class=\"python language-python\">dt = datetime.datetime(2001, 1, 31, 10, 51, 0)\r\n\r\nprint(dt.strftime('%Y-%m-%d::%H-%M'))\r\n#&gt; 2001-01-31::10-51\r\n<\/code><\/pre>\r\n<h2 id=\"7example2formattingadatetimeobject\">7. Example 2 &#8211; Formatting a datetime object<\/h2>\r\n<p>Parse the following datetime object to the following representation: &#8217;31 January, 2001, Wednesday&#8217;<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\ndt = datetime.datetime(2001, 1, 31)\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"> <strong>Solution:<\/strong><\/p>\r\n<pre><code class=\"python language-python\">dt.strftime('%d %B, %Y, %A')\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"8usefuldatetimefunctions\">8. Useful datetime functions<\/h2>\r\n<p>The datetime object contains a number of useful date-time related methods.<\/p>\r\n<pre><code class=\"python language-python\"># create a datatime obj\r\ndt = datetime.datetime(2019, 2, 15)\r\n\r\n# 1. Get the current day of the month\r\ndt.day #&gt; 31\r\n\r\n# 2. Get the current day of the week\r\ndt.isoweekday() #&gt; 5 --&gt; Friday\r\n\r\n# 3. Get the current month of the year \r\ndt.month  #&gt; 2 --&gt; February\r\n\r\n# 4. Get the Year\r\ndt.year  #&gt; 2019\r\n<\/code><\/pre>\r\n<h2 id=\"6whenandhowtousethedatetimetimeclass\">9. When and how to use the datetime.time() class?<\/h2>\r\n<p>The <code>datetime.time()<\/code> is used to represnt the time component alone, without the date. The defualt output format is: hours, minutes, seconds and microseconds.<\/p>\r\n<pre><code class=\"python language-python\"># hours, minutues, seconds, microseconds\r\ntm = datetime.time(10,40,10,102301)\r\ntm\r\n#&gt; datetime.time(10, 40, 10, 102301)\r\n<\/code><\/pre>\r\n<h2 id=\"7whenandhowtousethedatetimetimedeltaclass\">10. When and how to use the datetime.timedelta() class?<\/h2>\r\n<p>&#8216;TimeDeltas&#8217; represent a period of time that a particular time instance. You can think of them simply as the difference between two dates or times. It is normally used to add or remove a certain duration of time from <code>datetime<\/code> objects. To create a <code>datetime.timedelta<\/code> class you need to pass a specified duration to the class constructor. The arguments can be in <code>weeks<\/code>,<code>days<\/code> (default), <code>hours<\/code>, <code>minutes<\/code>, <code>seconds<\/code>, <code>microseconds<\/code>.<\/p>\r\n<pre><code class=\"python language-python\">td = datetime.timedelta(days=30)\r\ntd\r\n<\/code><\/pre>\r\n<p>Now I have a `timedelta` object that represents a duration of 30 days. Let&#8217;s compute the date will be 30 days from now.<\/p>\r\n<pre><code class=\"python language-python\">print(datetime.date.today() + td)\r\n#&gt; 2019-03-17\r\n<\/code><\/pre>\r\n<p>Likewise, you can subtract timedeltas as well. Another convenience with timedeltas is you can create arbitrary combination of time durations represented with days, weeks, hours etc. It will simplify that combination<\/p>\r\n<pre><code class=\"python language-python\">td = datetime.timedelta(weeks=1, days=30, hours=2, minutes=40)\r\ntd \r\n#&gt; datetime.timedelta(days=37, seconds=9600)\r\n<\/code><\/pre>\r\n<p>If you subtract two datetime objects you will get a timedelta object that represent the duration.<\/p>\r\n<pre><code class=\"python language-python\">dt1 = datetime.datetime(2002, 1, 31, 10, 10, 0)\r\ndt2 = datetime.datetime(2001, 1, 31, 10, 10, 0)\r\ndt1 - dt2\r\n#&gt; datetime.timedelta(days=365)\r\n<\/code><\/pre>\r\n<p>Likewise, you can subtract two time deltas to get another timedelta object.<\/p>\r\n<pre><code class=\"python language-python\">td1 = datetime.timedelta(days=30)  # 30 days\r\ntd2 = datetime.timedelta(weeks=1)  # 1 week\r\ntd1 - td2\r\n#&gt; datetime.timedelta(days=23)\r\n<\/code><\/pre>\r\n<h2 id=\"8howtoworkwithtimezones\">11. How to work with timezones?<\/h2>\r\n<p>For time zones, python recommends <code>pytz<\/code> module which is not a standard built-in library. You need to install it separately (enter `pip install pytz` in terminal or command prompt) So how to set time zone to a particular <code>datetime<\/code>? Simply pass the respective pytz <code>timezone<\/code> object to <code>tzinfo<\/code> parameter when you create the datetime. Then, that <code>datetime<\/code> will become timezone aware. Let&#8217;s create a datetime object that belongs to UTC timezone.<\/p>\r\n<pre><code class=\"python language-python\">import pytz\r\ndatetime.datetime(2001, 1, 31, 10, 10, 0, tzinfo=pytz.UTC)\r\n<\/code><\/pre>\r\n<p>UTC was a direct attribute of the <code>pytz<\/code> module. So, how to set to a different timezone? Lookup <code>pytz.all_timezones<\/code> for your timezone of choice. Then use the <code>pytz.timezone()<\/code> to create the respective timezone object that will be passed to the <code>tzinfo<\/code> argument.<\/p>\r\n<pre><code class=\"python language-python\"># See available time zones\r\npytz.all_timezones[:5]\r\n#&gt; ['Africa\/Abidjan',\r\n#&gt;  'Africa\/Accra',\r\n#&gt;  'Africa\/Addis_Ababa',\r\n#&gt;  'Africa\/Algiers',\r\n#&gt;  'Africa\/Asmara']\r\n<\/code><\/pre>\r\n<pre><code class=\"python language-python\"># Set to particular timezone\r\ndt_in = datetime.datetime(2001, 1, 31, 3, 30, 0, 0, tzinfo=pytz.timezone('Asia\/Tokyo'))\r\ndt_in\r\n#&gt; datetime.datetime(2001, 1, 31, 3, 30, tzinfo=&lt;DstTzInfo 'Asia\/Tokyo' LMT+9:19:00 STD&gt;)\r\n<\/code><\/pre>\r\n<p>You can know that by converting to respective target timezone.<\/p>\r\n<pre><code class=\"python language-python\">tgt_timezone = pytz.timezone('Africa\/Addis_Ababa')\r\ndt_in.astimezone(tgt_timezone)\r\n<\/code><\/pre>\r\n<h2 id=\"9practiceexamples\">12. Practice Examples<\/h2>\r\n<p>Rules for the challenges:<\/p>\r\n<ol>\r\n<li>No looking at the calendar<\/li>\r\n<li>Solve the problems with python code even if it is possible to compute it mentally<\/li>\r\n<\/ol>\r\n<h2 id=\"practiceexercise1howtoparsedatestringstodatetimeformat\">Exercise 1: How to parse date strings to datetime format?<\/h2>\r\n<p>Parse the following date strings to datetime format (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\ns1 = \"2010 Jan 1\"\r\ns2 = '31-1-2000' \r\ns3 = 'October10, 1996, 10:40pm'\r\n\r\n# Deisred Output\r\n#&gt; 2010-01-01 00:00:00\r\n#&gt; 2000-01-31 00:00:00\r\n#&gt; 2019-10-10 22:40:00\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\ns1 = \"2010 Jan 1\"\r\ns2 = '31-1-2000' \r\ns3 = 'October10,1996, 10:40pm'\r\n\r\n# Solution\r\nfrom dateutil.parser import parse\r\nprint(parse(s1))\r\nprint(parse(s2))\r\nprint(parse(s3))\r\n<\/code><\/pre>\r\n<pre><code>2010-01-01 00:00:00\r\n2000-01-31 00:00:00\r\n2019-10-10 22:40:00\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise2howmanydayshasitbeensinceyouwereborn\">Exercise 2: How many days has it been since you were born?<\/h2>\r\n<p>How many days has it been since you were born? (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nbday = 'Oct 2, 1869'  # use bday\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nbday = 'Oct 2, 1869'\r\n\r\nimport datetime\r\nfrom dateutil.parser import parse\r\n\r\n# Solution\r\ntd = datetime.datetime.now() - parse(bday)\r\ntd.days\r\n<\/code><\/pre>\r\n<pre><code>54558\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise3howtocountthenumberofsaturdaysbetweentwodates\">Exercise 3: How to count the number of saturdays between two dates?<\/h2>\r\n<p>Count the number of saturdays between two dates (medium)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\nd1 = datetime.date(1869, 1, 2)\r\nd2 = datetime.date(1869, 10, 2)\r\n\r\n# Desired Output\r\n#&gt; 40\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\nd1 = datetime.date(1869, 1, 2)\r\nd2 = datetime.date(1869, 10, 2)\r\n\r\n# Solution\r\ndelta = d2 - d1  # timedelta\r\n\r\n# Get all dates \r\ndates_btw_d1d2 = [(d1 + datetime.timedelta(i)) for i in range(delta.days + 1)]\r\n\r\nn_saturdays = 0\r\nfor d in dates_btw_d1d2:\r\n    n_saturdays += int(d.isoweekday() == 6)\r\n\r\nprint(n_saturdays)    \r\n<\/code><\/pre>\r\n<pre><code>40\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise4howmanydaysisituntilyournextbirthdaythisyear\">Exercise 4: How many days is it until your next birthday this year?<\/h2>\r\n<p>How many days is it until your next birthday this year? (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nbday = 'Oct 2, 1869'  # use b'day\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nbday = 'Oct 2, 1869'  # Enter birthday here\r\n\r\nimport datetime\r\nfrom dateutil.parser import parse\r\n\r\n# Solution\r\nbdate = parse(bday)\r\ncurrent_bdate = datetime.date(year=datetime.date.today().year, month=bdate.month, day=bdate.day) \r\ntd = current_bdate - datetime.date.today()\r\ntd.days\r\n<\/code><\/pre>\r\n<pre><code>228\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise5howtocountthenumberofdaysbetweensuccessivedaysinanirregularsequence\">Exercise 5: How to count the number of days between successive days in an irregular sequence?<\/h2>\r\n<p>Count the number of days between successive days in the following list. (medium)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\n['Oct, 2, 1869', 'Oct, 10, 1869', 'Oct, 15, 1869', 'Oct, 20, 1869', 'Oct, 23, 1869']\r\n\r\n# Desired Output\r\n#&gt; [8, 5, 5, 3]\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\ndatestrings = ['Oct, 2, 1869', 'Oct, 10, 1869', 'Oct, 15, 1869', 'Oct, 20, 1869', 'Oct, 23, 1869']\r\n\r\n# Solution\r\nimport datetime\r\nfrom dateutil.parser import parse\r\nimport numpy as np\r\n\r\ndates = [parse(d) for d in datestrings]\r\n\r\nprint([d.days for d in np.diff(dates)])\r\n<\/code><\/pre>\r\n<pre><code>[8, 5, 5, 3]\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise6howtoconvertnumberofdaystoseconds\">Exercise 6: How to convert number of days to seconds?<\/h2>\r\n<p>Convert the number of days till your next birthday to seconds (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\nbdate = datetime.date(1869, 10, 2)\r\ntd = datetime.date.today() - bdate\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\nbdate = datetime.date(1869, 10, 2)\r\ntd = datetime.date.today() - bdate\r\n\r\n# Solution\r\ntd.total_seconds()\r\n<\/code><\/pre>\r\n<pre><code>4713811200.0\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise7howtoconvertagivendatetoadatetimesetatthebeginningoftheday\">Exercise 7: How to convert a given date to a datetime set at the beginning of the day?<\/h2>\r\n<p>Convert a given date to a datetime set at the beginning of the day (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\ndate = datetime.date(1869, 10, 2)\r\n\r\n# Desired Output\r\n#&gt; 1869-10-02 00:00:00\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\">from datetime import date, datetime\r\nd = date(1869, 10, 2)\r\nprint(datetime.combine(d, datetime.min.time()))\r\n#&gt; 1869-10-02 00:00:00\r\n<\/code><\/pre>\r\n<pre><code>1869-10-02 00:00:00\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise8howtogetthelastdayofthemonthforanygivendateinpython\">Exercise 8: How to get the last day of the month for any given date in python?<\/h2>\r\n<p>Get the last day of the month for the below given date in python (easy)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\ndt = datetime.date(1952, 2, 12)\r\n\r\n# Desired Output\r\n#&gt; 29\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\ndt = datetime.date(1952, 2, 12)\r\n\r\n# Solution\r\nimport calendar\r\ncalendar.monthrange(dt.year,dt.month)[1]\r\n<\/code><\/pre>\r\n<pre><code>29\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise9howmanysundaysdoesthemonthoffebruary1948have\">Exercise 9: How many Sundays does the month of February 1948 have?<\/h2>\r\n<p>Count the Sundays does the month of February 1948 have? (medium) <details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\">import datetime\r\nfrom calendar import monthrange\r\n\r\nd1 = datetime.date(1948, 2, 1)\r\nn_days = monthrange(1948, 2)\r\n\r\n# Get all dates \r\ndates_btw_d1d2 = [(d1 + datetime.timedelta(i)) for i in range(n_days[1])]\r\n\r\nn_sundays = 0\r\nfor d in dates_btw_d1d2:\r\n    n_sundays += int(d.isoweekday() == 6)\r\n\r\nprint(n_sundays)    #&gt; 4\r\n<\/code><\/pre>\r\n<pre><code>4\r\n<\/code><\/pre>\r\n<p><\/div><\/details><\/p>\r\n<h2 id=\"practiceexercise10howtoformatagivendatetommmddyyyyfortmat\">Exercise 10: How to format a given date to &#8220;mmm-dd, YYYY&#8221; fortmat?<\/h2>\r\n<p>Format a given date to &#8220;mmm-dd, YYYY&#8221; fortmat? (easy)<\/p>\r\n<pre><code class=\"python language-python\"># input\r\nimport datetime\r\nd1 = datetime.date('2010-09-28')\r\n\r\n# Desired output\r\n#&gt; 'Sep-28, 2010'\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nimport datetime\r\nd1 = datetime.date(2010, 9, 28)\r\n\r\n# Solution\r\nd1.strftime('%b-%d, %Y')\r\n<\/code><\/pre>\r\n<pre><code>'Sep-28, 2010'\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise11howtoconvertdatetimetoyearqtrformat\">Exercise 11: How to convert datetime to Year-Qtr format?<\/h2>\r\n<p>Convert the below datetime to Year-Qtr format? (easy)<\/p>\r\n<pre><code class=\"python language-python\"># input\r\nimport datetime\r\nd1 = datetime.datetime(2010, 9, 28, 10, 40, 59)\r\n\r\n# Desired output\r\n#&gt; '2010-Q3'\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># input\r\nimport datetime\r\nd1 = datetime.datetime(2010, 9, 28, 10, 40, 59)\r\n\r\n# Solution\r\nf'{d1.year}-Q{d1.month\/\/4 + 1}'\r\n<\/code><\/pre>\r\n<pre><code>'2010-Q3'\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise12howtoconvertunixtimestamptoareadabledate\">Exercise 12: How to convert unix timestamp to a readable date?<\/h2>\r\n<p>Convert the below unix timestamp to a readable date (medium)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nunixtimestamp = 528756281\r\n\r\n# Desired Output\r\n#&gt; 04-October-1986\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\nunixtimestamp = 528756281\r\n\r\n# Solution\r\nimport datetime\r\ndt = datetime.datetime.fromtimestamp(528756281)\r\ndt.strftime('%d-%B-%Y')\r\n<\/code><\/pre>\r\n<pre><code>'04-October-1986'\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise13howtogetthetimeinadifferenttimezone\">Exercise 13: How to get the time in a different timezone?<\/h2>\r\n<p>If it is &#8216;2001-01-31::3:30:0&#8217; in &#8216;Asia\/Tokyo&#8217;. What time is it in &#8216;Asia\/Kolkata&#8217;? (medium)<\/p>\r\n<pre><code class=\"python language-python\">import datetime\r\ndt_in = datetime.datetime(2001, 1, 31, 3, 30, 0, 0, tzinfo=pytz.timezone('Asia\/Tokyo'))\r\n\r\n# Desired Solution\r\n#&gt; datetime.datetime(2001, 1, 30, 23, 41, tzinfo=&lt;DstTzInfo 'Asia\/Kolkata' IST+5:30:00 STD&gt;)\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\">import datetime\r\ndt_in = datetime.datetime(2001, 1, 31, 3, 30, 0, 0, tzinfo=pytz.timezone('Asia\/Tokyo'))\r\n\r\n# Solution\r\nindia_tz = pytz.timezone('Asia\/Kolkata')\r\ndt_in.astimezone(india_tz)\r\n<\/code><\/pre>\r\n<pre><code>datetime.datetime(2001, 1, 30, 23, 41, tzinfo=&lt;DstTzInfo 'Asia\/Kolkata' IST+5:30:00 STD&gt;)\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"practiceexercise14howtofillupmissingdatesinagivenirregularsequenceofdates\">Exercise 14: How to fill up missing dates in a given irregular sequence of dates?<\/h2>\r\n<p>Fill up missing dates in a given irregular sequence of dates? (hard)<\/p>\r\n<pre><code class=\"python language-python\"># Input\r\n['Oct 2, 1869', 'Oct 5, 1869', 'Oct 7, 1869', 'Oct 9, 1869']\r\n\r\n# Desired Output\r\n#&gt; ['Oct 02, 1869', 'Oct 03, 1869', 'Oct 04, 1869', 'Oct 05, 1869', \r\n#&gt; 'Oct 06, 1869', 'Oct 07, 1869', 'Oct 08, 1869', 'Oct 09, 1869']\r\n<\/code><\/pre>\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Show Solution<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n<pre><code class=\"python language-python\"># Input\r\ndatestrings = ['Oct 2, 1869', 'Oct 5, 1869', 'Oct 7, 1869', 'Oct 9, 1869']\r\n\r\n# Solution\r\nimport datetime\r\nfrom dateutil.parser import parse\r\nimport numpy as np\r\n\r\ndates = [parse(d) for d in datestrings]\r\n\r\nd1 = np.min(dates)\r\nd2 = np.max(dates)\r\n\r\ndelta = d2 - d1  # timedelta\r\n\r\n# Get all dates \r\ndates_btw_d1d2 = [(d1 + datetime.timedelta(i)).strftime('%b %d, %Y') for i in range(delta.days + 1)]\r\nprint(dates_btw_d1d2)\r\n<\/code><\/pre>\r\n<pre><code>['Oct 02, 1869', 'Oct 03, 1869', 'Oct 04, 1869', 'Oct 05, 1869', 'Oct 06, 1869', 'Oct 07, 1869', 'Oct 08, 1869', 'Oct 09, 1869']\r\n<\/code><\/pre>\r\n<p><\/div><\/details>\r\n<h2 id=\"10conclusion\">10. Conclusion<\/h2>\r\n<p>How many were you able to solve? Congratulations if you were able to solve 7 or more. We covered nearly everything you will need to work with dates in python. Let me know if I have missed anything. Or if you have better answers or have more questions, please write in the comments area below. See you in the next one! \u00a0<\/p>","protected":false},"excerpt":{"rendered":"<p>datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python. datetime in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2093,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[21],"tags":[79,22],"class_list":["post-2080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-datetime","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>datetime in Python - Simplified Guide with Clear Examples - ML+<\/title>\n<meta name=\"description\" content=\"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/python\/datetime-python-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"datetime in Python - Simplified Guide with Clear Examples - ML+\" \/>\n<meta property=\"og:description\" content=\"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/python\/datetime-python-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/rtipaday\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-13T08:49:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-08T17:05:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/localhost:8080\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"560\" \/>\n\t<meta property=\"og:image:height\" content=\"315\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Selva Prabhakaran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/R_Programming\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Selva Prabhakaran\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/\"},\"author\":{\"name\":\"Selva Prabhakaran\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\"},\"headline\":\"datetime in Python &#8211; Simplified Guide with Clear Examples\",\"datePublished\":\"2019-04-13T08:49:56+00:00\",\"dateModified\":\"2022-03-08T17:05:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/\"},\"wordCount\":1487,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/datetime-in-Python.png\",\"keywords\":[\"datetime\",\"Python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/\",\"name\":\"datetime in Python - Simplified Guide with Clear Examples - ML+\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/datetime-in-Python.png\",\"datePublished\":\"2019-04-13T08:49:56+00:00\",\"dateModified\":\"2022-03-08T17:05:08+00:00\",\"description\":\"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/datetime-python-examples\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/datetime-in-Python.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2019\\\/04\\\/datetime-in-Python.png\",\"width\":560,\"height\":315,\"caption\":\"datetime in Python\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/510885c0515804366fa644c38258391e\",\"name\":\"Selva Prabhakaran\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207\",\"caption\":\"Selva Prabhakaran\"},\"description\":\"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \\\/ ML \\\/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.\",\"sameAs\":[\"https:\\\/\\\/localhost:8080\\\/\",\"https:\\\/\\\/www.facebook.com\\\/rtipaday\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/R_Programming\"],\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/selva86\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"datetime in Python - Simplified Guide with Clear Examples - ML+","description":"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.","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:\/\/localhost:8080\/python\/datetime-python-examples\/","og_locale":"en_US","og_type":"article","og_title":"datetime in Python - Simplified Guide with Clear Examples - ML+","og_description":"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.","og_url":"https:\/\/localhost:8080\/python\/datetime-python-examples\/","og_site_name":"machinelearningplus","article_author":"https:\/\/www.facebook.com\/rtipaday\/","article_published_time":"2019-04-13T08:49:56+00:00","article_modified_time":"2022-03-08T17:05:08+00:00","og_image":[{"width":560,"height":315,"url":"https:\/\/localhost:8080\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png","type":"image\/png"}],"author":"Selva Prabhakaran","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/R_Programming","twitter_misc":{"Written by":"Selva Prabhakaran","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/"},"author":{"name":"Selva Prabhakaran","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e"},"headline":"datetime in Python &#8211; Simplified Guide with Clear Examples","datePublished":"2019-04-13T08:49:56+00:00","dateModified":"2022-03-08T17:05:08+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/"},"wordCount":1487,"commentCount":2,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png","keywords":["datetime","Python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/python\/datetime-python-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/","url":"https:\/\/localhost:8080\/python\/datetime-python-examples\/","name":"datetime in Python - Simplified Guide with Clear Examples - ML+","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/#primaryimage"},"image":{"@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png","datePublished":"2019-04-13T08:49:56+00:00","dateModified":"2022-03-08T17:05:08+00:00","description":"datetime is the standard module for working with dates in python. It provides 4 main objects for date and time operations: datetime, date, time and timedelta. In this post you will learn how to do all sorts of operations with these objects and solve date-time related practice problems (easy to hard) in Python.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/python\/datetime-python-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/localhost:8080\/python\/datetime-python-examples\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2019\/04\/datetime-in-Python.png","width":560,"height":315,"caption":"datetime in Python"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/510885c0515804366fa644c38258391e","name":"Selva Prabhakaran","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/a994280177da541405c016f593e86ea7.jpg?ver=1776363207","caption":"Selva Prabhakaran"},"description":"Selva is an experienced Data Scientist and leader, specializing in executing AI projects for large companies. Selva started machinelearningplus to make Data Science \/ ML \/ AI accessible to everyone. The website enjoys 4 Million+ readership. His courses, lessons, and videos are loved by hundreds of thousands of students and practitioners.","sameAs":["https:\/\/localhost:8080\/","https:\/\/www.facebook.com\/rtipaday\/","https:\/\/x.com\/https:\/\/twitter.com\/R_Programming"],"url":"https:\/\/machinelearningplus.com\/author\/selva86\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/2080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=2080"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/2080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/2093"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=2080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=2080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=2080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}