{"id":4824,"date":"2017-12-09T00:44:02","date_gmt":"2017-12-08T19:14:02","guid":{"rendered":"https:\/\/www.java2blog.com\/?p=4824"},"modified":"2023-11-25T09:16:54","modified_gmt":"2023-11-25T03:46:54","slug":"python-string-to-datetime","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-string-to-datetime\/","title":{"rendered":"Python String to datetime"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Usingstrptime_method\">Using\u00a0strptime method<\/a><\/li><li><a href=\"#Usingparser\">Using\u00a0parser<\/a><\/li><\/ul><\/div>\n<p>In this tutorial, we will see how to convert String to datetime object.<br \/>\nLet&#8217;s say, you got String as input and you want to convert it datetime object to extract various details from it.<\/p>\n<ul>\n<li>Using\u00a0strptime<\/li>\n<li>Using parser<\/li>\n<\/ul>\n<h2><span id=\"Usingstrptime_method\"><span style=\"color: #f89820;\">Using\u00a0strptime method<\/span><\/span><\/h2>\n<p>You can simply use strptime to convert String to datetime.<br \/>\nLet&#8217;s understand with the help of example.<\/p>\n<pre code=\"python\">\nfrom datetime import datetime\n\ndatetime_object = datetime.strptime('Apr 15 2019 8:29PM', '%b %d %Y %I:%M%p')\nprint(\"datetime Obj:\",datetime_object)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">datetime Obj: 2019-04-15 20:29:00<\/div>\n<p>Let&#8217;s understand mening of %b,%d,%Y,%I,%M and %p<\/p>\n<ul>\n<li>%b Month as locale\u2019s abbreviated name(Apr)<\/li>\n<li>%d Day of the month as a zero-padded decimal number(15)<\/li>\n<li>%Y Year with century as a decimal number(2019)<\/li>\n<li>%I Hour (12-hour clock) as a zero-padded decimal number(8)<\/li>\n<li>%M Minute as a zero-padded decimal number(29)<\/li>\n<li>%p Locale\u2019s equivalent of either AM or PM(PM)<\/li>\n<\/ul>\n<h2><span id=\"Usingparser\"><span style=\"color: #f89820;\">Using\u00a0parser<\/span><\/span><\/h2>\n<p>You can use third party module dateUtil to get object of datetime object.<\/p>\n<pre code=\"python\">\nfrom dateutil import parser\ndt = parser.parse(\"Apr 15 2019  8:29PM\")\nprint(\"datetime Obj:\",dt)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">datetime Obj: 2019-04-15 20:29:00<\/div>\n<p>That&#8217;s all about converting String to datetime in python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing\u00a0strptime methodUsing\u00a0parser In this tutorial, we will see how to convert String to datetime object. Let&#8217;s say, you got String as input and you want to convert it datetime object to extract various details from it. Using\u00a0strptime Using parser Using\u00a0strptime method You can simply use strptime to convert String to datetime. Let&#8217;s understand [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4824"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=4824"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4824\/revisions"}],"predecessor-version":[{"id":25701,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/4824\/revisions\/25701"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=4824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=4824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=4824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}