{"id":74241,"date":"2025-03-04T17:26:25","date_gmt":"2025-03-04T11:56:25","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=74241"},"modified":"2025-10-07T17:49:35","modified_gmt":"2025-10-07T12:19:35","slug":"handling-json-datetime-between-python-and-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/handling-json-datetime-between-python-and-javascript\/","title":{"rendered":"Handling JSON datetime between Python and JavaScript"},"content":{"rendered":"\n<p>Have you ever tried sending a date from Python to JavaScript or vice versa, only to realize the formats don&#8217;t match? You&#8217;re not alone! Handling JSON datetime between Python and JavaScript can be tricky because each language handles date and time differently.&nbsp;<\/p>\n\n\n\n<p>In this blog, we\u2019ll break it down step by step, helping you understand how to convert Python datetime to JSON, parse JSON date format in JavaScript, and bridge the gap between these two powerful languages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Handling<\/strong> <strong>JSON Datetime Between Python and JavaScript<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python and JSON Datetime<\/strong><\/h3>\n\n\n\n<p>In Python, working with dates and times is done using the datetime module. However, when serializing datetime objects to JSON, we need to convert them into a format that JSON understands, such as ISO 8601 format.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Converting Python Datetime to JSON<\/strong><\/h4>\n\n\n\n<p>Python&#8217;s json module doesn\u2019t support datetime objects directly. If you try to serialize a datetime object, you\u2019ll get an error:<\/p>\n\n\n\n<p><em>import json<\/em><\/p>\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\"><div class=\"wp-block-group__inner-container\">\n<p><em>from datetime import datetime<\/em><\/p>\n\n\n\n<p><em>data = {&#8220;timestamp&#8221;: datetime.now()}<\/em><\/p>\n\n\n\n<p><em>json.dumps(data)&nbsp; # Raises TypeError<\/em><\/p>\n\n\n\n<p>To fix this, we need to convert the datetime object to a string in ISO format:<\/p>\n\n\n\n<p><em>data = {&#8220;timestamp&#8221;: datetime.now().isoformat()}<\/em><\/p>\n\n\n\n<p><em>json_data = json.dumps(data)<\/em><\/p>\n\n\n\n<p><em>print(json_data)<\/em><\/p>\n\n\n\n<p>This outputs something like:<\/p>\n\n\n\n<p><em>{&#8220;timestamp&#8221;: &#8220;2025-03-01T12:34:56.789123&#8221;}<\/em><\/p>\n<\/div><\/div>\n\n\n\n<p>This format (ISO 8601) is widely accepted and works well with JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>JavaScript and JSON Datetime Parsing<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript\u2019s<\/a> Date object makes it easy to handle dates and times, but parsing JSON datetime strings requires special attention.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>JavaScript Datetime Parse<\/strong><\/h4>\n\n\n\n<p>Let\u2019s assume we received the JSON datetime from Python:<\/p>\n\n\n\n<p><em>{&#8220;timestamp&#8221;: &#8220;2025-03-01T12:34:56.789123&#8221;}<\/em><\/p>\n\n\n\n<p>We can parse this in JavaScript using:<\/p>\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\"><div class=\"wp-block-group__inner-container\">\n<p><em>const jsonData = &#8216;{&#8220;timestamp&#8221;: &#8220;2025-03-01T12:34:56.789123&#8221;}&#8217;;<\/em><\/p>\n\n\n\n<p><em>const parsedData = JSON.parse(jsonData);<\/em><\/p>\n\n\n\n<p><em>const dateObject = new Date(parsedData.timestamp);<\/em><\/p>\n\n\n\n<p><em>console.log(dateObject);&nbsp; \/\/ Sat Mar 01 2025 12:34:56 GMT+0000 (UTC)<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Differences Between Python and JavaScript Datetime Handling<\/strong><\/h3>\n<\/div><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Feature<\/strong><\/td><td><strong>Python (<\/strong><strong>datetime<\/strong><strong>)<\/strong><\/td><td><strong>JavaScript (<\/strong><strong>Date<\/strong><strong>)<\/strong><\/td><\/tr><tr><td>Default Format<\/td><td>Not JSON serializable<\/td><td>ISO 8601 (String)<\/td><\/tr><tr><td>Conversion Needed<\/td><td>.isoformat()<\/td><td>new Date() parsing<\/td><\/tr><tr><td>Timezone Handling<\/td><td>Supports UTC &amp; offsets<\/td><td>Local timezone by default<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Want to explore JavaScript in-depth? Do register for HCL GUVI\u2019s <a href=\"https:\/\/www.guvi.in\/courses\/web-development\/javascript\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=Handling-JSON-datetime-between-Python-and-JavaScript\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/courses\/web-development\/javascript\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=Handling-JSON-datetime-between-Python-and-JavaScript\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript self-paced course<\/a> where you will learn concepts such as the working of JavaScript and its many benefits, Variables, Objects, Operators, and Functions, as well as advanced topics like Closures, Hoisting, and Classes to name a few. You will also learn concepts pertaining to the latest update of ES6.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Handling datetime in JSON between Python and JavaScript is all about using ISO 8601 format. Python requires explicit conversion using .isoformat(), while JavaScript can directly parse and work with ISO format dates. Understanding these differences ensures smooth data exchange between backend and frontend applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1741002158222\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. Why does Python throw an error when trying to serialize datetime to JSON?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python&#8217;s json module doesn\u2019t support datetime objects natively. You need to convert them to a string using .isoformat() before serialization.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1741002163303\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. How do I ensure the datetime is in UTC when converting from Python?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use .isoformat() with timezone-aware datetime objects:<br \/><em>from datetime import datetime, timezone<\/em><br \/><em>dt = datetime.now(timezone.utc)<\/em><br \/><em>json.dumps({&#8220;timestamp&#8221;: dt.isoformat()})<\/em><\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1741002178420\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. How can I format a JavaScript Date object back into JSON format?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use .toISOString():<br \/><em>const now = new Date();<\/em><br \/><em>console.log(now.toISOString());<\/em><br \/>This ensures compatibility with Python and other systems.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1741002192030\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. What is the best format to store datetime in JSON?<\/strong> <\/h3>\n<div class=\"rank-math-answer \">\n\n<p>ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) is the best format as it is universally accepted and easy to parse in both Python and JavaScript.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Have you ever tried sending a date from Python to JavaScript or vice versa, only to realize the formats don&#8217;t match? You&#8217;re not alone! Handling JSON datetime between Python and JavaScript can be tricky because each language handles date and time differently.&nbsp; In this blog, we\u2019ll break it down step by step, helping you understand [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":74292,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717,429],"tags":[],"views":"3618","authorinfo":{"name":"Isha Sharma","url":"https:\/\/www.guvi.in\/blog\/author\/isha\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/03\/JSON-datetime-between-Python-and-JavaScript-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/03\/JSON-datetime-between-Python-and-JavaScript.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=74241"}],"version-history":[{"count":9,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions"}],"predecessor-version":[{"id":89009,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions\/89009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/74292"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=74241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=74241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=74241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}