{"id":35674,"date":"2023-12-01T05:11:56","date_gmt":"2023-12-01T05:11:56","guid":{"rendered":"https:\/\/accuweb.cloud\/resource\/faq\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\/"},"modified":"2026-02-19T11:33:59","modified_gmt":"2026-02-19T11:33:59","slug":"how-to-convert-a-string-to-a-datetime-or-time-object-in-python","status":"publish","type":"faq","link":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python","title":{"rendered":"How To Convert a String to a datetime or time Object in Python"},"content":{"rendered":"<h2 class=\"ack-h2\">How To Convert a String to a DateTime or Time Object in Python<\/h2>\n<p>In Python, the <strong>`datetime`<\/strong> and `time` modules offer a convenient <strong>`strptime()`<\/strong> class method that allows you to convert strings into corresponding objects effortlessly.<\/p>\n<p>Parsing a string into a datetime object with datetime.strptime()<\/p>\n<p><b>Syntax<\/b><\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\ndatetime.strptime(date_string, format)<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>The <strong>`datetime.strptime()`<\/strong> method returns a<strong> `datetime`<\/strong> object corresponding to the `date_string`, interpreting it based on the specified format. It&#8217;s important to note that both `date_string` and the format must be provided as string arguments.<\/p>\n<h2>The Default Format<\/h2>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\n'%a %b %d %H:%M:%S %Y'<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>You can convert a string to a <b><strong>datetime<\/strong><\/b> or <b><strong>time<\/strong><\/b> <b><strong>object<\/strong><\/b> in Python using the <b><strong>datetime module<\/strong><\/b>, which provides various functions and classes for working with date and time values. <b><strong>Here&#8217;s how you can do it:<\/strong><\/b><br \/>\n<a class=\"ack-link-color\" href=\"https:\/\/accuweb.cloud\/application\/python-hosting\">Check our developer-friendly Python Hosting!<\/a><\/p>\n<h3 class=\"ack-h3\">1. Convert String to datetime.datetime() Object<\/h3>\n<p>Python&#8217;s standard library includes the datetime module, allowing you to manipulate dates and times effectively. With the help of the datetime class, you can easily transform a string into a datetime object. Let&#8217;s break down the fundamental elements in a straightforward manner:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\ndatetime.strptime(date_string, format_string):<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<h3>This Method takes Two Arguments<\/h3>\n<p><strong>date_string:<\/strong> The input string of date or datetime<\/p>\n<p><strong>format_string:<\/strong> A string that specifies the format of the input string. It contains placeholders (format codes) that match the components in the input string.<\/p>\n<p>In the next illustration, a date and time string gets transformed into a `datetime.datetime()` object, and then we display both the class name and the value of the resulting object.<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nfrom datetime import datetime\r\ndatetime_str = '09\/19\/23 13:55:26'\r\nformat_string = \"%m\/%d\/%y %H:%M:%S\"\r\ndatetime_object = datetime.strptime(datetime_str, format_string)\r\nprint(type(datetime_object))\r\nprint(datetime_object)\u00a0 # printed in default format<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><\/p>\n<p>&lt;class &#8216;datetime.datetime&#8217;&gt;2023-09-19 13:55:2600<br \/>\n%Y: Represents the year<\/p>\n<p>%m: Represents the month as a zero-padded decimal number.<br \/>\n%d: Represents the day of the month as a zero-padded decimal number.<br \/>\n%H: Represents the hour (24-hour clock) as a zero-padded decimal number.<br \/>\n%M: Represents the minute as a zero-padded decimal number.<br \/>\n%S: Represents the second as a zero-padded decimal number.<\/p>\n<h3 class=\"ack-h3\">2. Convert String to datetime.date() Object<\/h3>\n<p>You can employ a similar method to extract the time component without the date. In this case, you can use the .time() method on the resultant datetime object to obtain a time object, which solely represents the time part, comprising hours, minutes, seconds, and microseconds. This approach proves valuable when you focus on time-related computations and don&#8217;t require the date information.<\/p>\n<p>It&#8217;s crucial to ensure that the format string you provide precisely matches the format of your input string. A mismatch will cause the datetime.strptime() method to raise a ValueError exception.<\/p>\n<p>Python&#8217;s datetime module equips you with robust tools for managing dates and times, enabling you to efficiently and accurately handle various datetime-related tasks.<\/p>\n<p>As an example, let&#8217;s take a look at the following illustration. In this case, a date string is converted into a `datetime.date()` object. The example showcases the resulting object&#8217;s class type and its corresponding value:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nfrom datetime import datetime\r\ndateStr = '09-19-2023'\r\ndateObj = datetime.strptime(dateStr, '%m-%d-%Y').date()\r\nprint(type(dateObj))\r\nprint(dateObj)\u00a0 # printed in default format\r\n<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><br \/>\n&lt;class &#8216;datetime.date&#8217;2023-09-19<\/p>\n<div class=\"cta-btn-top-space ack-extra-image-space\">\t\t<div data-elementor-type=\"section\" data-elementor-id=\"38668\" class=\"elementor elementor-38668\" data-elementor-settings=\"{&quot;ha_cmc_init_switcher&quot;:&quot;no&quot;}\" data-elementor-post-type=\"elementor_library\">\n\t\t\t        <section class=\"elementor-section elementor-top-section elementor-element elementor-element-882321f elementor-section-boxed elementor-section-height-default elementor-section-height-default ct-header-fixed-none ct-row-max-none\" data-id=\"882321f\" data-element_type=\"section\" data-settings=\"{&quot;_ha_eqh_enable&quot;:false}\">\n            \n                        <div class=\"elementor-container elementor-column-gap-default \">\n                    <div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7cc79cc\" data-id=\"7cc79cc\" data-element_type=\"column\">\n        <div class=\"elementor-widget-wrap elementor-element-populated\">\n                    \n        \t\t<div class=\"elementor-element elementor-element-e31b40f elementor-widget elementor-widget-shortcode\" data-id=\"e31b40f\" data-element_type=\"widget\" data-widget_type=\"shortcode.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-shortcode\"><\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t            <\/div>\n        <\/div>\n                    <\/div>\n        <\/section>\n        \t\t<\/div>\n\t\t<\/div>\n<div class=\"cta-btn-bottom-space\"><\/div>\n<h3 class=\"ack-h3\">3. Convert String to datetime.time() Object<\/h3>\n<p>The subsequent illustration showcases the conversion of a time string into a datetime.time() object. It then displays the class type and the value of the resultant object:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nfrom datetime import datetime\r\ntime_str = '13::55::26'\r\ntime_object = datetime.strptime(time_str, '%H::%M::%S').time()\r\nprint(type(time_object))\r\nprint(time_object) <\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><br \/>\n&lt;class &#8216;datetime.time&#8217;&gt;13:55:26<\/p>\n<h3 class=\"ack-h3\">4. Convert String to datetime.datetime() Object with Locale<\/h3>\n<p>The given instance demonstrates the conversion of a date string in a German locale into a datetime.datetime() object. Subsequently, it displays both the class type and the value of the resulting object:<\/p>\n<div class=\"article-space\"><\/div>\n<div class=\"ack-formula\"><b>Note:<\/b> If the locale you need is unavailable, you may need to generate it. You can use the locale-gen command on some systems to generate the desired locale. For example, on a Debian-based system, you can run:<\/div>\n<div class=\"article-extra-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nsudo dpkg-reconfigure locales<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>If &#8216;en_US.UTF-8&#8217; is unavailable, you can try using a different locale supported on your system. For example, you can use &#8216;en_US.utf8&#8217; (lowercase &#8216;utf8&#8217; instead of &#8216;UTF-8&#8217;):<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nfrom datetime import datetime\r\nimport locale\r\nlocale.setlocale(locale.LC_ALL, 'en_US.utf8')\r\ndate_str_de_DE = '16-Dezember-2022 Freitag'\u00a0 # de_DE locale\r\ndatetime_object = datetime.strptime(date_str_de_DE, '%d-%B-%Y %A')\r\nprint(type(datetime_object))\r\nprint(datetime_object)<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><br \/>\n&lt;class &#8216;datetime.datetime&#8217;&gt;2022-12-16 00:00:00<\/p>\n<p>It&#8217;s important to highlight that the result does not include the weekday name extracted from the input string. A `datetime.datetime()` object only represents the weekday as a numerical value.<br \/>\n<a class=\"ack-link-color\" href=\"https:\/\/accuweb.cloud\/register\">Sign up&nbsp;and avail $100 free credits now!!<\/a><\/p>\n<h3 class=\"ack-h3\">5. Converting a String to a struct_time() Object Using time.strptime()<\/h3>\n<p><b>Syntax<\/b><\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">  \r\ntime.strptime(time_string[, format])<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>The<strong> `time.strptime()`<\/strong> method generates a<strong> `time.struct_time()`<\/strong> object, which is constructed based on the<strong> `time_string`<\/strong> and its interpretation according to the provided format. For a precise extraction of time data, it&#8217;s crucial that both <strong>`time_string`<\/strong> and `format` are supplied as string inputs. If no format is explicitly specified, a default format will be applied.<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\n'%a %b %d %H:%M:%S %Y'<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>This corresponds to the format returned by the <strong>ctime()<\/strong> function.<\/p>\n<p>The format directives remain consistent for both <strong>`time.strptime()`<\/strong> and <strong>`time.strftime()`<\/strong>. To delve deeper into these format directives for the time module, you can refer to the Python documentation for comprehensive information.<\/p>\n<div class=\"tooltip-container\">\n<div class=\"tooltip-text\">\nTo convert a string to a datetime or time object in Python, use datetime.strptime() with the appropriate format string. This allows you to parse the string into a datetime object, which can then be used for time-based operations.<\/div>\n<div class=\"main-tooltip-btn\">\n<a class=\"tooltip-link\" href=\"https:\/\/accuweb.cloud\/application\/python-hosting\" target=\"_blank\" rel=\"noopener\"><button class=\"tooltip-btn\">Python Hosting <svg class=\"tooltip-arrow\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 448 512\" style=\"width: 20px; height: 20px;\">\n        <path d=\"M438.6 278.6c12.5-12.5 12.5-32.8 0-45.3l-160-160c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L338.8 224 32 224c-17.7 0-32 14.3-32 32s14.3 32 32 32l306.7 0L233.4 393.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l160-160z\"><\/path>\n    <\/svg><\/button><\/a>\n<\/div>\n<\/div>\n<h3 class=\"ack-h3\">6. Convert String to struct_time() Object With Format Provided<\/h3>\n<p>Here&#8217;s an example that converts a time string into a `time.struct_time()` object, utilizing the format argument, and subsequently displays the value of the resulting object:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nimport time\r\ntime_str = '11::33::54'\r\ntime_obj = time.strptime(time_str, '%H::%M::%S')\r\nprint(\"A time.struct_time object that uses the format provided:\")\r\nprint(time_obj)<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is:<\/strong><\/p>\n<p>A time.struct_time object that uses the format provided:<\/p>\n<p>time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1,<br \/>\ntm_hour=11, tm_min=33, tm_sec=54, tm_wday=0, tm_yday=1,<br \/>\ntm_isdst=-1)<\/p>\n<p>The output shows that when you convert a string into a <strong>`time.struct_time()`<\/strong> object, the <strong>`strptime()`<\/strong> method employs placeholder values for any format directives not specified in the format argument.<br \/>\n<a class=\"ack-link-color\" href=\"https:\/\/accuweb.cloud\/application\/python-hosting\">Check our developer-friendly Python Hosting!<\/a><\/p>\n<h3 class=\"ack-h3\">7. Convert String to struct_time() Object Using Default Format<\/h3>\n<p>The default format is applied if you omit the format argument when converting a time string into a `time.struct_time()` object. In this case, an error will be raised if the input string does not precisely match the default format. The default format is:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\n'%a %b %d %H:%M:%S %Y'<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p>In the following example, a time string is converted into a time.struct_time() object without any format argument and the result is printed:<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nimport time # default format - \"%a %b %d %H:%M:%S %Y\"\r\ntime_str_default = 'Mon Dec 12 14:55:02 2022'\r\ntime_obj_default = time.strptime(time_str_default)\r\nprint(\"A time.struct_time object that uses the default format:\")\r\nprint(time_obj_default)<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><br \/>\nA time.struct_time object that uses the default format:<br \/>\ntime.struct_time(tm_year=2022, tm_mon=12, tm_mday=12,<br \/>\ntm_hour=14, tm_min=55, tm_sec=2, tm_wday=0, tm_yday=346,<br \/>\ntm_isdst=-1)<\/p>\n<p>In the output, you can see that the strptime() method uses placeholder values for any format instructions not specified in the format argument or in the default format when no format is specified.<\/p>\n<h3 class=\"ack-h3\">8. Troubleshooting strptime() Errors<\/h3>\n<p>When the <strong>`strptime()`<\/strong> method fails to interpret the input string by the specified format, a ValueError is raised. To detect any parsing errors, one can use a try-except block to observe the output. The ValueError generated by `strptime()` provides detailed information regarding the cause of the parsing issue. The following example outlines some of the more common errors, such as having extra data and format mismatches.<\/p>\n<div class=\"article-space\"><\/div>\n<pre><code class=\"language-javascript\">\r\nimport time\r\nfrom datetime import datetime\r\ndatetime_str = '09\/19\/18 13:55:26'\r\ntry:\r\ndatetime_object = datetime.strptime(datetime_str, '%m\/%d\/%y')\r\nexcept ValueError as ve1:\r\nprint('ValueError 1:', ve1)\r\ntime_str = '99::55::26'\r\ntry:\r\ntime_object = time.strptime(time_str, '%H::%M::%S')\r\nexcept ValueError as ve2:\r\nprint('ValueError 2:', ve2)<\/code><\/pre>\n<div class=\"article-space\"><\/div>\n<p><strong>The output is<\/strong><br \/>\nValueError 1: unconverted data remains:\u00a0 13:55:26<br \/>\nValueError 2: time data &#8217;99::55::26&#8242; does not match format &#8216;%H::%M::%S&#8217;<br \/>\n<a class=\"ack-link-color\" href=\"https:\/\/accuweb.cloud\/register\">Register and get Auto Scalable instances with a Pay-As-You-Go Pricing Model!<\/a><\/p>\n<h2 class=\"ack-h2\">Conclusion<\/h2>\n<p>Throughout this guide, you learned how to transform date and time strings into datetime and time objects using Python.<\/p>\n<div class=\"cta-btn-top-space ack-extra-image-space\">\t\t<div data-elementor-type=\"section\" data-elementor-id=\"38668\" class=\"elementor elementor-38668\" data-elementor-settings=\"{&quot;ha_cmc_init_switcher&quot;:&quot;no&quot;}\" data-elementor-post-type=\"elementor_library\">\n\t\t\t        <section class=\"elementor-section elementor-top-section elementor-element elementor-element-882321f elementor-section-boxed elementor-section-height-default elementor-section-height-default ct-header-fixed-none ct-row-max-none\" data-id=\"882321f\" data-element_type=\"section\" data-settings=\"{&quot;_ha_eqh_enable&quot;:false}\">\n            \n                        <div class=\"elementor-container elementor-column-gap-default \">\n                    <div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-7cc79cc\" data-id=\"7cc79cc\" data-element_type=\"column\">\n        <div class=\"elementor-widget-wrap elementor-element-populated\">\n                    \n        \t\t<div class=\"elementor-element elementor-element-e31b40f elementor-widget elementor-widget-shortcode\" data-id=\"e31b40f\" data-element_type=\"widget\" data-widget_type=\"shortcode.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-shortcode\"><\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t            <\/div>\n        <\/div>\n                    <\/div>\n        <\/section>\n        \t\t<\/div>\n\t\t<\/div>\n<div class=\"cta-btn-bottom-space\"><\/div>\n","protected":false},"author":1,"featured_media":52879,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","class_list":["post-35674","faq","type-faq","status-publish","has-post-thumbnail","hentry","faq_topics-kb","faq_topics-product-documentation","faq_topics-python-series","faq_topics-python-string","faq_topics-tutorial-series","faq_topics-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.10 (Yoast SEO v24.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Convert a String to a datetime or time Object in Python - AccuWeb Cloud<\/title>\n<meta name=\"description\" content=\"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Convert a String to a datetime or time Object in Python\" \/>\n<meta property=\"og:description\" content=\"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\" \/>\n<meta property=\"og:site_name\" content=\"AccuWeb Cloud\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-19T11:33:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#article\",\"isPartOf\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\"},\"author\":{\"name\":\"Jilesh Patadiya\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/a7a4cbe8405202b537509c757b588c58\"},\"headline\":\"How To Convert a String to a datetime or time Object in Python\",\"datePublished\":\"2023-12-01T05:11:56+00:00\",\"dateModified\":\"2026-02-19T11:33:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\"},\"wordCount\":1227,\"publisher\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/#organization\"},\"image\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg\",\"inLanguage\":\"en-US\"},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\",\"url\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\",\"name\":\"How To Convert a String to a datetime or time Object in Python - AccuWeb Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage\"},\"image\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage\"},\"thumbnailUrl\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg\",\"datePublished\":\"2023-12-01T05:11:56+00:00\",\"dateModified\":\"2026-02-19T11:33:59+00:00\",\"description\":\"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage\",\"url\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg\",\"contentUrl\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/accuweb.cloud\/resource\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How To Convert a String to a datetime or time Object in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#website\",\"url\":\"https:\/\/accuweb.cloud\/resource\/\",\"name\":\"AccuWeb Cloud\",\"description\":\"Cutting Edge Cloud Computing\",\"publisher\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/accuweb.cloud\/resource\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#organization\",\"name\":\"AccuWeb.Cloud\",\"url\":\"https:\/\/accuweb.cloud\/resource\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/04\/accuwebcloud_logo_black_tagline.jpg\",\"contentUrl\":\"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/04\/accuwebcloud_logo_black_tagline.jpg\",\"width\":156,\"height\":87,\"caption\":\"AccuWeb.Cloud\"},\"image\":{\"@id\":\"https:\/\/accuweb.cloud\/resource\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/a7a4cbe8405202b537509c757b588c58\",\"name\":\"Jilesh Patadiya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2cea2bdb5bbabb771ee67e96acad7396f25cb1a0c360b9bc4a9ac40cea9cd8b2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2cea2bdb5bbabb771ee67e96acad7396f25cb1a0c360b9bc4a9ac40cea9cd8b2?s=96&d=mm&r=g\",\"caption\":\"Jilesh Patadiya\"},\"description\":\"Jilesh Patadiya, the visionary Co-Founder and Chief Technology Officer (CTO) behind AccuWeb.Cloud. Founder &amp; CTO at AccuWebHosting.com. He shares his web hosting insights on the AccuWeb.Cloud blog. He mostly writes on the latest web hosting trends, WordPress, storage technologies, and Windows and Linux hosting platforms.\",\"sameAs\":[\"https:\/\/accuweb.cloud\/resource\",\"https:\/\/www.facebook.com\/accuwebhosting\",\"https:\/\/www.instagram.com\/accuwebhosting\/\",\"https:\/\/www.linkedin.com\/company\/accuwebhosting\/\",\"https:\/\/x.com\/accuwebhosting\",\"https:\/\/www.youtube.com\/c\/Accuwebhosting\"],\"url\":\"https:\/\/accuweb.cloud\/resource\/author\/accuwebadmin\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How To Convert a String to a datetime or time Object in Python - AccuWeb Cloud","description":"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.","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:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python","og_locale":"en_US","og_type":"article","og_title":"How To Convert a String to a datetime or time Object in Python","og_description":"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.","og_url":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python","og_site_name":"AccuWeb Cloud","article_modified_time":"2026-02-19T11:33:59+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#article","isPartOf":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python"},"author":{"name":"Jilesh Patadiya","@id":"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/a7a4cbe8405202b537509c757b588c58"},"headline":"How To Convert a String to a datetime or time Object in Python","datePublished":"2023-12-01T05:11:56+00:00","dateModified":"2026-02-19T11:33:59+00:00","mainEntityOfPage":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python"},"wordCount":1227,"publisher":{"@id":"https:\/\/accuweb.cloud\/resource\/#organization"},"image":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage"},"thumbnailUrl":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg","inLanguage":"en-US"},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python","url":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python","name":"How To Convert a String to a datetime or time Object in Python - AccuWeb Cloud","isPartOf":{"@id":"https:\/\/accuweb.cloud\/resource\/#website"},"primaryImageOfPage":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage"},"image":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage"},"thumbnailUrl":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg","datePublished":"2023-12-01T05:11:56+00:00","dateModified":"2026-02-19T11:33:59+00:00","description":"Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples.","breadcrumb":{"@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#primaryimage","url":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg","contentUrl":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/07\/NEW-OG-IMAGE-URL.jpg","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/accuweb.cloud\/resource\/articles\/how-to-convert-a-string-to-a-datetime-or-time-object-in-python#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/accuweb.cloud\/resource\/"},{"@type":"ListItem","position":2,"name":"How To Convert a String to a datetime or time Object in Python"}]},{"@type":"WebSite","@id":"https:\/\/accuweb.cloud\/resource\/#website","url":"https:\/\/accuweb.cloud\/resource\/","name":"AccuWeb Cloud","description":"Cutting Edge Cloud Computing","publisher":{"@id":"https:\/\/accuweb.cloud\/resource\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/accuweb.cloud\/resource\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/accuweb.cloud\/resource\/#organization","name":"AccuWeb.Cloud","url":"https:\/\/accuweb.cloud\/resource\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/accuweb.cloud\/resource\/#\/schema\/logo\/image\/","url":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/04\/accuwebcloud_logo_black_tagline.jpg","contentUrl":"https:\/\/accuweb.cloud\/resource\/wp-content\/uploads\/2024\/04\/accuwebcloud_logo_black_tagline.jpg","width":156,"height":87,"caption":"AccuWeb.Cloud"},"image":{"@id":"https:\/\/accuweb.cloud\/resource\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/a7a4cbe8405202b537509c757b588c58","name":"Jilesh Patadiya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/accuweb.cloud\/resource\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2cea2bdb5bbabb771ee67e96acad7396f25cb1a0c360b9bc4a9ac40cea9cd8b2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2cea2bdb5bbabb771ee67e96acad7396f25cb1a0c360b9bc4a9ac40cea9cd8b2?s=96&d=mm&r=g","caption":"Jilesh Patadiya"},"description":"Jilesh Patadiya, the visionary Co-Founder and Chief Technology Officer (CTO) behind AccuWeb.Cloud. Founder &amp; CTO at AccuWebHosting.com. He shares his web hosting insights on the AccuWeb.Cloud blog. He mostly writes on the latest web hosting trends, WordPress, storage technologies, and Windows and Linux hosting platforms.","sameAs":["https:\/\/accuweb.cloud\/resource","https:\/\/www.facebook.com\/accuwebhosting","https:\/\/www.instagram.com\/accuwebhosting\/","https:\/\/www.linkedin.com\/company\/accuwebhosting\/","https:\/\/x.com\/accuwebhosting","https:\/\/www.youtube.com\/c\/Accuwebhosting"],"url":"https:\/\/accuweb.cloud\/resource\/author\/accuwebadmin"}]}},"_links":{"self":[{"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/faq\/35674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/faq"}],"about":[{"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/types\/faq"}],"author":[{"embeddable":true,"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/comments?post=35674"}],"version-history":[{"count":8,"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/faq\/35674\/revisions"}],"predecessor-version":[{"id":53488,"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/faq\/35674\/revisions\/53488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/media\/52879"}],"wp:attachment":[{"href":"https:\/\/accuweb.cloud\/resource\/wp-json\/wp\/v2\/media?parent=35674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}