{"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":"2026-06-09T09:51:46","modified_gmt":"2026-06-09T04:21:46","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: A Complete 2026 Guide"},"content":{"rendered":"\n<p>Have you ever tried sending a date from Python to JavaScript or vice versa, only to realize the formats do not match? You are not alone. Handling JSON datetime between Python and JavaScript can be tricky because each language handles date and time differently. The datetime object in Python is not natively JSON serializable, while JavaScript uses its own Date format that can behave inconsistently across environments.<\/p>\n\n\n\n<p>In this blog, we will break it down step by step, helping you understand how to convert Python datetime to JSON, parse JSON date format in JavaScript, manage timezones correctly, and handle common errors that come up when handling JSON datetime between Python and JavaScript in production applications. By the end, you will have a complete, working reference for handling JSON datetime between Python and JavaScript in any full-stack project.<\/p>\n\n\n\n<p><strong>Quick Answer:<\/strong> <\/p>\n\n\n\n<p>Handling JSON datetime between Python and JavaScript requires converting Python datetime objects to ISO 8601 strings using <em><strong>.isoformat()<\/strong><\/em> before serialization, and then parsing those strings in JavaScript using <em><strong>new Date()<\/strong><\/em> or <em><strong>Date.parse()<\/strong><\/em>. The ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ) is the universally accepted standard for handling JSON datetime between Python and JavaScript reliably across timezones and environments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Handling JSON Datetime Between Python and JavaScript Is Tricky<\/h2>\n\n\n\n<p>Before diving into the code, it helps to understand why handling JSON datetime between Python and JavaScript is a common source of bugs in full-stack applications.<\/p>\n\n\n\n<p>Python&#8217;s <em><strong>datetime<\/strong><\/em> module stores dates as objects with rich metadata: year, month, day, hour, minute, second, microsecond, and optional timezone info. Python&#8217;s <em><strong>json<\/strong><\/em> module has no idea what to do with these objects by default and throws a <em><strong>TypeError<\/strong><\/em> if you try to serialize them directly.<\/p>\n\n\n\n<p>JavaScript&#8217;s <em><strong>Date<\/strong><\/em> object stores dates as milliseconds since the Unix epoch (January 1, 1970). When parsing strings, JavaScript is relatively flexible but can behave inconsistently depending on the format and the browser or Node.js version.<\/p>\n\n\n\n<p>The bridge between the two is <strong>ISO 8601<\/strong>, an international standard format that both Python and JavaScript understand and produce reliably. Mastering this format is the core of handling JSON datetime between Python and JavaScript correctly.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Problem<\/th><th>Python Behavior<\/th><th>JavaScript Behavior<\/th><\/tr><\/thead><tbody><tr><td>Native datetime in JSON<\/td><td>Throws TypeError<\/td><td>Date object not JSON serializable either<\/td><\/tr><tr><td>Preferred string format<\/td><td>ISO 8601 via .isoformat()<\/td><td>ISO 8601 via .toISOString()<\/td><\/tr><tr><td>Timezone handling<\/td><td>Naive (no timezone) by default<\/td><td>Local timezone by default<\/td><\/tr><tr><td>UTC output<\/td><td>Requires explicit timezone.utc<\/td><td>Requires .toISOString() which always outputs UTC<\/td><\/tr><tr><td>Microseconds<\/td><td>Supported (6 decimal places)<\/td><td>Not supported (3 decimal places max)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>Think about this: handling JSON datetime between Python and JavaScript is essentially a translation problem. Both languages speak their own dialect of time, and ISO 8601 is the common language they share.<\/em><\/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\">Using a Custom JSON Encoder for Datetime<\/h3>\n\n\n\n<p>For larger applications where you want to serialize datetime objects automatically without manually calling <em><strong>.isoformat()<\/strong><\/em> everywhere, you can write a custom JSON encoder. This is the production-ready approach for handling JSON datetime between Python and JavaScript at scale, especially in Flask or Django APIs that return many datetime fields:<\/p>\n\n\n\n<p><em><strong>import json<\/strong><\/em> <em><strong>from datetime import datetime<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>class DateTimeEncoder(json.JSONEncoder):<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>def default(self, obj):<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>if isinstance(obj, datetime):<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>return obj.isoformat()<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>return super().default(obj)<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>data = {&#8220;timestamp&#8221;: datetime.now(), &#8220;event&#8221;: &#8220;user_login&#8221;}<\/strong><\/em> <em><strong>json_data = json.dumps(data, cls=DateTimeEncoder)<\/strong><\/em> <em><strong>print(json_data)<\/strong><\/em><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><em><strong>{&#8220;timestamp&#8221;: &#8220;2026-03-01T12:34:56.789123&#8221;, &#8220;event&#8221;: &#8220;user_login&#8221;}<\/strong><\/em><\/p>\n\n\n\n<p>This approach is especially useful in Flask or Django backends where you need consistent datetime serialization across multiple API endpoints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parsing JSON Datetime Back in Python<\/h3>\n\n\n\n<p>When receiving a JSON datetime string from JavaScript or any other source, Python needs to parse it back into a datetime object:<\/p>\n\n\n\n<p><em><strong>from datetime import datetime<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>timestamp_str = &#8220;2026-03-01T12:34:56.789123&#8221;<\/strong><\/em> <em><strong>dt = datetime.fromisoformat(timestamp_str)<\/strong><\/em> <em><strong>print(dt) # 2026-03-01 12:34:56.789123<\/strong><\/em><\/p>\n\n\n\n<p>For ISO 8601 strings with a UTC &#8216;Z&#8217; suffix (sent from JavaScript&#8217;s <em><strong>.toISOString()<\/strong><\/em>):<\/p>\n\n\n\n<p><em><strong>timestamp_str = &#8220;2026-03-01T12:34:56.000Z&#8221;<\/strong><\/em> <em><strong>dt = datetime.fromisoformat(timestamp_str.replace(&#8220;Z&#8221;, &#8220;+00:00&#8221;))<\/strong><\/em> <em><strong>print(dt) # 2026-03-01 12:34:56+00:00<\/strong><\/em><\/p>\n\n\n\n<p><em>Note: Python 3.11 and above can parse the &#8216;Z&#8217; suffix directly without the replace workaround.<\/em><\/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\">Converting JavaScript Date Back to ISO String for Python<\/h3>\n\n\n\n<p>When sending a datetime from JavaScript to a Python backend, always use <em><strong>.toISOString()<\/strong><\/em> to ensure you produce a format Python can reliably parse:<\/p>\n\n\n\n<p><em><strong>const now = new Date();<\/strong><\/em> <em><strong>const isoString = now.toISOString();<\/strong><\/em> <em><strong>console.log(isoString);<\/strong><\/em> <em><strong>\/\/ &#8220;2026-03-01T12:34:56.789Z&#8221;<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>\/\/ Send to Python backend:<\/strong><\/em> <em><strong>fetch(&#8216;\/api\/data&#8217;, {<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>method: &#8216;POST&#8217;,<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>headers: { &#8216;Content-Type&#8217;: &#8216;application\/json&#8217; },<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>body: JSON.stringify({ timestamp: isoString })<\/strong><\/em> <em><strong>});<\/strong><\/em><\/p>\n\n\n\n<p>On the Python backend:<\/p>\n\n\n\n<p><em><strong>from datetime import datetime<\/strong><\/em> <em><strong>import json<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>body = &#8216;{&#8220;timestamp&#8221;: &#8220;2026-03-01T12:34:56.789Z&#8221;}&#8217;<\/strong><\/em> <em><strong>data = json.loads(body)<\/strong><\/em> <em><strong>dt = datetime.fromisoformat(data[&#8220;timestamp&#8221;].replace(&#8220;Z&#8221;, &#8220;+00:00&#8221;))<\/strong><\/em> <em><strong>print(dt) # 2026-03-01 12:34:56+00:00<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Handling Timezones When Working with JSON Datetime Between Python and JavaScript<\/h2>\n\n\n\n<p>Timezone handling is the most error-prone part of handling JSON datetime between Python and JavaScript. The golden rule is: always store and transmit datetimes in UTC, and convert to local time only for display. This single rule eliminates 90% of timezone bugs in handling JSON datetime between Python and JavaScript.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Making Python Datetime Timezone-Aware (UTC)<\/h3>\n\n\n\n<p>A naive datetime in Python has no timezone info. Always use timezone-aware datetimes for API communication:<\/p>\n\n\n\n<p><em><strong>from datetime import datetime, timezone<\/strong><\/em><\/p>\n\n\n\n<p><em><strong># Naive datetime (avoid for APIs)<\/strong><\/em> <em><strong>naive_dt = datetime.now()<\/strong><\/em> <em><strong>print(naive_dt.isoformat())<\/strong><\/em> <em><strong># &#8220;2026-03-01T12:34:56.789123&#8221; \u2014 no timezone info<\/strong><\/em><\/p>\n\n\n\n<p><em><strong># Timezone-aware datetime (correct for handling JSON datetime between Python and JavaScript)<\/strong><\/em> <em><strong>aware_dt = datetime.now(timezone.utc)<\/strong><\/em> <em><strong>print(aware_dt.isoformat())<\/strong><\/em> <em><strong># &#8220;2026-03-01T12:34:56.789123+00:00&#8221; \u2014 explicit UTC<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>json.dumps({&#8220;timestamp&#8221;: aware_dt.isoformat()})<\/strong><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">JavaScript Timezone Gotchas<\/h3>\n\n\n\n<p>JavaScript&#8217;s <em><strong>new Date()<\/strong><\/em> without an explicit timezone always uses the local system timezone. This is a common source of bugs when handling JSON datetime between Python and JavaScript in multi-timezone applications:<\/p>\n\n\n\n<p><em><strong>\/\/ Problem: Parses in LOCAL timezone<\/strong><\/em> <em><strong>const local = new Date(&#8220;2026-03-01T12:34:56&#8221;);<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>\/\/ Safe: Always use UTC-explicit strings from Python<\/strong><\/em> <em><strong>const utc = new Date(&#8220;2026-03-01T12:34:56+00:00&#8221;);<\/strong><\/em> <em><strong>const utcZ = new Date(&#8220;2026-03-01T12:34:56.000Z&#8221;);<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>\/\/ Display in user&#8217;s local timezone (correct approach)<\/strong><\/em> <em><strong>console.log(utcZ.toLocaleString(&#8220;en-IN&#8221;, { timeZone: &#8220;Asia\/Kolkata&#8221; }));<\/strong><\/em> <em><strong>\/\/ Shows time in IST for Indian users<\/strong><\/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><thead><tr><th>Feature<\/th><th>Python (datetime)<\/th><th>JavaScript (Date)<\/th><\/tr><\/thead><tbody><tr><td>Default Format<\/td><td>Not JSON serializable<\/td><td>ISO 8601 (String via .toISOString())<\/td><\/tr><tr><td>Conversion Needed<\/td><td>.isoformat()<\/td><td>new Date() parsing<\/td><\/tr><tr><td>Timezone Handling<\/td><td>Naive by default (no timezone)<\/td><td>Local timezone by default<\/td><\/tr><tr><td>UTC Output<\/td><td>datetime.now(timezone.utc)<\/td><td>new Date().toISOString() always UTC<\/td><\/tr><tr><td>Microseconds<\/td><td>Supported (6 decimal places)<\/td><td>Not supported (3 decimal places)<\/td><\/tr><tr><td>Parsing back<\/td><td>datetime.fromisoformat()<\/td><td>new Date(string)<\/td><\/tr><tr><td>JSON serialization<\/td><td>Requires custom encoder<\/td><td>JSON.stringify() handles Date as string<\/td><\/tr><tr><td>Best format for exchange<\/td><td>ISO 8601 with +00:00 offset<\/td><td>ISO 8601 with Z suffix<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Errors and How to Fix Them<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>TypeError: Object of type datetime is not JSON serializable<\/td><td>Direct json.dumps on datetime object<\/td><td>Convert with .isoformat() or use custom encoder<\/td><\/tr><tr><td>Invalid Date in JavaScript<\/td><td>Python sent naive datetime without timezone<\/td><td>Use datetime.now(timezone.utc).isoformat()<\/td><\/tr><tr><td>Time is off by hours<\/td><td>Timezone mismatch between systems<\/td><td>Always use UTC for storage and transmission<\/td><\/tr><tr><td>Python fromisoformat fails on &#8220;Z&#8221; suffix<\/td><td>Python &lt; 3.11 does not parse &#8220;Z&#8221;<\/td><td>Replace &#8220;Z&#8221; with &#8220;+00:00&#8221; before parsing<\/td><\/tr><tr><td>Microseconds causing JavaScript parse error<\/td><td>Python sends 6 decimal places<\/td><td>Truncate to 3 using .strftime or slice the string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>This errors table covers the five bugs developers encounter most often when handling JSON datetime between Python and JavaScript in real projects.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real-World Example: Full-Stack API Flow<\/strong><\/h2>\n\n\n\n<p>Here is a complete end-to-end example showing handling JSON datetime between Python and JavaScript in a Flask and JavaScript application. This pattern covers the full round trip and is the recommended approach for handling JSON datetime between Python and JavaScript in any REST API project:<\/p>\n\n\n\n<p><strong>Python Flask Backend:<\/strong><\/p>\n\n\n\n<p><em><strong>from flask import Flask, jsonify, request<\/strong><\/em> <em><strong>from datetime import datetime, timezone<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>app = Flask(name)<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>@app.route(&#8216;\/api\/event&#8217;, methods=[&#8216;GET&#8217;])<\/strong><\/em> <em><strong>def get_event():<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>event = {<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>&#8220;name&#8221;: &#8220;User Login&#8221;,<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>&#8220;timestamp&#8221;: datetime.now(timezone.utc).isoformat()<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>}<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>return jsonify(event)<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>@app.route(&#8216;\/api\/event&#8217;, methods=[&#8216;POST&#8217;])<\/strong><\/em> <em><strong>def post_event():<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>data = request.get_json()<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>ts_str = data.get(&#8220;timestamp&#8221;, &#8220;&#8221;)<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>dt = datetime.fromisoformat(ts_str.replace(&#8220;Z&#8221;, &#8220;+00:00&#8221;))<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>return jsonify({&#8220;received_at&#8221;: dt.isoformat(), &#8220;status&#8221;: &#8220;ok&#8221;})<\/strong><\/em><\/p>\n\n\n\n<p><strong>JavaScript Frontend:<\/strong><\/p>\n\n\n\n<p><em><strong>\/\/ GET: Receive and display datetime from Python<\/strong><\/em> <em><strong>fetch(&#8216;\/api\/event&#8217;)<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>.then(res =&gt; res.json())<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>.then(data =&gt; {<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>const dt = new Date(data.timestamp);<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em><strong>console.log(&#8220;Event time (local):&#8221;, dt.toLocaleString());<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>});<\/strong><\/em><\/p>\n\n\n\n<p><em><strong>\/\/ POST: Send datetime from JavaScript to Python<\/strong><\/em> <em><strong>const payload = { timestamp: new Date().toISOString() };<\/strong><\/em> <em><strong>fetch(&#8216;\/api\/event&#8217;, {<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>method: &#8216;POST&#8217;,<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>headers: { &#8216;Content-Type&#8217;: &#8216;application\/json&#8217; },<\/strong><\/em> &nbsp;&nbsp;&nbsp;&nbsp;<em><strong>body: JSON.stringify(payload)<\/strong><\/em> <em><strong>});<\/strong><\/em><\/p>\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<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px; margin: 22px auto;\">\n  <h3 style=\"margin-top: 0; font-size: 22px; font-weight: 700; color: #ffffff;\">\ud83d\udca1 Did You Know?<\/h3>\n  <ul style=\"padding-left: 20px; margin: 10px 0;\">\n    <li>The ISO 8601 standard that underpins handling JSON datetime between Python and JavaScript was first published by the International Organization for Standardization in 1988 and has been the global standard for date and time representation in software ever since. Every developer working on full-stack projects should know this standard cold before attempting handling JSON datetime between Python and JavaScript in production.<\/li>\n    <li>Python 3.11 (released in 2022 and now the dominant production version in 2026) finally added native support for parsing ISO 8601 strings with the &#8220;Z&#8221; suffix, eliminating the most common workaround required when handling JSON datetime between Python and JavaScript.<\/li>\n  <\/ul>\n<\/div>\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 do not match? You are not alone. Handling JSON datetime between Python and JavaScript can be tricky because each language handles date and time differently. The datetime object in Python is not natively JSON serializable, while [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":74292,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717,429],"tags":[],"views":"4060","authorinfo":{"name":"Jebasta","url":"https:\/\/www.guvi.in\/blog\/author\/jebasta\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/03\/JSON-datetime-between-Python-and-JavaScript-300x116.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\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=74241"}],"version-history":[{"count":12,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions"}],"predecessor-version":[{"id":115458,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions\/115458"}],"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}]}}