{"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-09-23T13:04:09","modified_gmt":"2026-09-23T07:34:09","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<h3 class=\"wp-block-heading\">AI Overview<\/h3>\n\n\n\n<p>Handling JSON datetime between Python and JavaScript means converting date and time values into a consistent string format that both environments can exchange reliably. For most APIs, use an ISO 8601 datetime with an explicit UTC offset, such as <code>2026-09-23T12:30:00.000Z<\/code>, and convert it back into a native datetime object when processing the response.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Python vs JavaScript Datetime Handling<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Python<\/th><th>JavaScript<\/th><\/tr><\/thead><tbody><tr><td>Native type<\/td><td><code>datetime<\/code><\/td><td><code>Date<\/code><\/td><\/tr><tr><td>JSON serialization<\/td><td>Convert <code>datetime<\/code> to a string or use a custom encoder<\/td><td><code>JSON.stringify()<\/code> serializes <code>Date<\/code> using <code>toJSON()<\/code><\/td><\/tr><tr><td>Recommended API format<\/td><td>ISO 8601<\/td><td>ISO 8601<\/td><\/tr><tr><td>UTC conversion<\/td><td><code>datetime.now(timezone.utc)<\/code><\/td><td><code>new Date().toISOString()<\/code><\/td><\/tr><tr><td>Convert to ISO string<\/td><td><code>.isoformat()<\/code><\/td><td><code>.toISOString()<\/code><\/td><\/tr><tr><td>Parse ISO datetime<\/td><td><code>datetime.fromisoformat()<\/code><\/td><td><code>new Date()<\/code><\/td><\/tr><tr><td>JSON module<\/td><td><code>json<\/code><\/td><td>Built-in <code>JSON<\/code><\/td><\/tr><tr><td>Subsecond precision<\/td><td>Microseconds supported<\/td><td>ISO <code>Date<\/code> output uses milliseconds<\/td><\/tr><tr><td>Main timezone concern<\/td><td>Naive vs timezone-aware datetime<\/td><td>UTC vs local display\/parsing behavior<\/td><\/tr><\/tbody><\/table><\/figure>\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><a href=\"https:\/\/www.guvi.in\/blog\/working-with-dates-in-javascript\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/working-with-dates-in-javascript\/\">JavaScript&#8217;s <em><strong>Date<\/strong><\/em><\/a> 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><a href=\"https:\/\/www.iso.org\/iso-8601-date-and-time-format.html\" target=\"_blank\" rel=\"noopener\">ISO 8601<\/a><\/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<pre class=\"wp-block-code\"><code>import json\n\nfrom datetime import datetime\n\ndata = {\"timestamp\": datetime.now()}\n\njson.dumps(data)&nbsp; # Raises TypeError\n\nTo fix this, we need to convert the datetime object to a string in ISO format:\n\ndata = {\"timestamp\": datetime.now().isoformat()}\n\njson_data = json.dumps(data)\n\nprint(json_data)\n\nThis outputs something like:\n\n{\"timestamp\": \"2025-03-01T12:34:56.789123\"}<\/code><\/pre>\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 <a href=\"https:\/\/www.guvi.in\/blog\/complete-guide-on-how-to-open-a-json-file\/\">JSON <\/a>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<pre class=\"wp-block-code\"><code>import json from datetime import datetime\n\nclass DateTimeEncoder(json.JSONEncoder): &nbsp;&nbsp;&nbsp;&nbsp;def default(self, obj): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if isinstance(obj, datetime): &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return obj.isoformat() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return super().default(obj)\n\ndata = {\"timestamp\": datetime.now(), \"event\": \"user_login\"} json_data = json.dumps(data, cls=DateTimeEncoder) print(json_data)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em><strong>{\"timestamp\": \"2026-03-01T12:34:56.789123\", \"event\": \"user_login\"}<\/strong><\/em><\/code><\/pre>\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<pre class=\"wp-block-code\"><code><em><strong>timestamp_str = \"2026-03-01T12:34:56.789123\"<\/strong><\/em> <em><strong>dt = datetime.fromisoformat(timestamp_str)<\/strong><\/em> <em><strong>print(dt) # 2026-03-01 12:34:56.789123<\/strong><\/em><\/code><\/pre>\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<pre class=\"wp-block-code\"><code><em><strong>timestamp_str = \"2026-03-01T12:34:56.000Z\"<\/strong><\/em> <em><strong>dt = datetime.fromisoformat(timestamp_str.replace(\"Z\", \"+00:00\"))<\/strong><\/em> <em><strong>print(dt) # 2026-03-01 12:34:56+00:00<\/strong><\/em><\/code><\/pre>\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<pre class=\"wp-block-code\"><code>from datetime import datetime import json\n\nbody = '{\"timestamp\": \"2026-03-01T12:34:56.789Z\"}' data = json.loads(body) dt = datetime.fromisoformat(data&#91;\"timestamp\"].replace(\"Z\", \"+00:00\")) print(dt) # 2026-03-01 12:34:56+00:00<\/code><\/pre>\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<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<pre class=\"wp-block-code\"><code>const now = new Date(); const isoString = now.toISOString(); console.log(isoString); \/\/ \"2026-03-01T12:34:56.789Z\"\n\n\/\/ Send to Python backend: fetch('\/api\/data', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'POST', &nbsp;&nbsp;&nbsp;&nbsp;headers: { 'Content-Type': 'application\/json' }, &nbsp;&nbsp;&nbsp;&nbsp;body: JSON.stringify({ timestamp: isoString }) });<\/code><\/pre>\n\n\n\n<p>On the Python backend:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from datetime import datetime import json\n\nbody = '{\"timestamp\": \"2026-03-01T12:34:56.789Z\"}' data = json.loads(body) dt = datetime.fromisoformat(data&#91;\"timestamp\"].replace(\"Z\", \"+00:00\")) print(dt) # 2026-03-01 12:34:56+00:00<\/code><\/pre>\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<pre class=\"wp-block-code\"><code>from datetime import datetime, timezone\n\n# Naive datetime (avoid for APIs) naive_dt = datetime.now() print(naive_dt.isoformat()) # \"2026-03-01T12:34:56.789123\" \u2014 no timezone info\n\n# Timezone-aware datetime (correct for handling JSON datetime between Python and JavaScript) aware_dt = datetime.now(timezone.utc) print(aware_dt.isoformat()) # \"2026-03-01T12:34:56.789123+00:00\" \u2014 explicit UTC\n\njson.dumps({\"timestamp\": aware_dt.isoformat()})<\/code><\/pre>\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<pre class=\"wp-block-code\"><code>\/\/ Problem: Parses in LOCAL timezone const local = new Date(\"2026-03-01T12:34:56\");\n\n\/\/ Safe: Always use UTC-explicit strings from Python const utc = new Date(\"2026-03-01T12:34:56+00:00\"); const utcZ = new Date(\"2026-03-01T12:34:56.000Z\");\n\n\/\/ Display in user's local timezone (correct approach) console.log(utcZ.toLocaleString(\"en-IN\", { timeZone: \"Asia\/Kolkata\" })); \/\/ Shows time in IST for Indian users<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is the Best Datetime Format for Python and JavaScript?<\/strong><\/h2>\n\n\n\n<p>For most Python-to-JavaScript APIs, use an ISO 8601 datetime string with an explicit timezone, preferably UTC.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-09-23T12:30:00.000Z\n<\/code><\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul>\n<li><code>2026-09-23<\/code> is the date.<\/li>\n\n\n\n<li><code>T<\/code> separates the date and time.<\/li>\n\n\n\n<li><code>12:30:00.000<\/code> represents the time and milliseconds.<\/li>\n\n\n\n<li><code>Z<\/code> indicates UTC.<\/li>\n<\/ul>\n\n\n\n<p>Python can produce an ISO representation using <code>datetime.isoformat()<\/code>, while JavaScript&#8217;s <code>Date.toISOString()<\/code> produces the UTC form <code>YYYY-MM-DDTHH:mm:ss.sssZ<\/code>.<\/p>\n\n\n\n<p>For API communication, keep the transmitted value timezone-aware. Convert it to the user&#8217;s local timezone only when displaying it in the interface.<\/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\">Common Errors and How to Fix Them<\/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>Complete Guide to Handling JSON Datetime in Python and JavaScript in India 2026<\/strong><\/h2>\n\n\n\n<p>For developers building applications for users in India, the same API principle applies: store and transmit timestamps consistently, preferably using UTC, and convert them to Indian Standard Time (IST) only when displaying them.<\/p>\n\n\n\n<p>For example, a UTC timestamp:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-09-23T07:00:00.000Z\n<\/code><\/pre>\n\n\n\n<p>can be displayed as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>23 September 2026, 12:30 PM IST\n<\/code><\/pre>\n\n\n\n<p>in an Indian user interface.<\/p>\n\n\n\n<p>In JavaScript, <code>Intl.DateTimeFormat<\/code> can be used for display:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const date = new Date(\"2026-09-23T07:00:00.000Z\");\n\nconst formatted = new Intl.DateTimeFormat(\"en-IN\", {\n    dateStyle: \"medium\",\n    timeStyle: \"short\",\n    timeZone: \"Asia\/Kolkata\"\n}).format(date);\n\nconsole.log(formatted);\n<\/code><\/pre>\n\n\n\n<p>The important distinction is that <strong>UTC should be used for consistent data exchange, while IST is a presentation timezone for Indian users<\/strong>.<\/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<pre class=\"wp-block-code\"><code>from flask import Flask, jsonify, request from datetime import datetime, timezone\n\napp = Flask(name)\n\n@app.route('\/api\/event', methods=&#91;'GET']) def get_event(): &nbsp;&nbsp;&nbsp;&nbsp;event = { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"name\": \"User Login\", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"timestamp\": datetime.now(timezone.utc).isoformat() &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return jsonify(event)\n\n@app.route('\/api\/event', methods=&#91;'POST']) def post_event(): &nbsp;&nbsp;&nbsp;&nbsp;data = request.get_json() &nbsp;&nbsp;&nbsp;&nbsp;ts_str = data.get(\"timestamp\", \"\") &nbsp;&nbsp;&nbsp;&nbsp;dt = datetime.fromisoformat(ts_str.replace(\"Z\", \"+00:00\")) &nbsp;&nbsp;&nbsp;&nbsp;return jsonify({\"received_at\": dt.isoformat(), \"status\": \"ok\"})<\/code><\/pre>\n\n\n\n<p><strong>JavaScript Frontend:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ GET: Receive and display datetime from Python fetch('\/api\/event') &nbsp;&nbsp;&nbsp;&nbsp;.then(res =&gt; res.json()) &nbsp;&nbsp;&nbsp;&nbsp;.then(data =&gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const dt = new Date(data.timestamp); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(\"Event time (local):\", dt.toLocaleString()); &nbsp;&nbsp;&nbsp;&nbsp;});\n\n\/\/ POST: Send datetime from JavaScript to Python const payload = { timestamp: new Date().toISOString() }; fetch('\/api\/event', { &nbsp;&nbsp;&nbsp;&nbsp;method: 'POST', &nbsp;&nbsp;&nbsp;&nbsp;headers: { 'Content-Type': 'application\/json' }, &nbsp;&nbsp;&nbsp;&nbsp;body: JSON.stringify(payload) });<\/code><\/pre>\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":"5273","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":17,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions"}],"predecessor-version":[{"id":140288,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/74241\/revisions\/140288"}],"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}]}}