
Handling JSON datetime between Python and JavaScript
Mar 04, 2025 2 Min Read 546 Views
(Last Updated)
Have you ever tried sending a date from Python to JavaScript or vice versa, only to realize the formats don’t match? You’re not alone! Handling JSON datetime between Python and JavaScript can be tricky because each language handles date and time differently.
In this blog, we’ll 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.
Table of contents
- Handling JSON Datetime Between Python and JavaScript
- Python and JSON Datetime
- JavaScript and JSON Datetime Parsing
- Differences Between Python and JavaScript Datetime Handling
- Conclusion
- FAQs
- Why does Python throw an error when trying to serialize datetime to JSON?
- How do I ensure the datetime is in UTC when converting from Python?
- How can I format a JavaScript Date object back into JSON format?
- What is the best format to store datetime in JSON?
Handling JSON Datetime Between Python and JavaScript
Python and JSON Datetime
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.
Converting Python Datetime to JSON
Python’s json module doesn’t support datetime objects directly. If you try to serialize a datetime object, you’ll get an error:
import json
from datetime import datetime
data = {“timestamp”: datetime.now()}
json.dumps(data) # Raises TypeError
To fix this, we need to convert the datetime object to a string in ISO format:
data = {“timestamp”: datetime.now().isoformat()}
json_data = json.dumps(data)
print(json_data)
This outputs something like:
{“timestamp”: “2025-03-01T12:34:56.789123”}
This format (ISO 8601) is widely accepted and works well with JavaScript.
JavaScript and JSON Datetime Parsing
JavaScript’s Date object makes it easy to handle dates and times, but parsing JSON datetime strings requires special attention.
JavaScript Datetime Parse
Let’s assume we received the JSON datetime from Python:
{“timestamp”: “2025-03-01T12:34:56.789123”}
We can parse this in JavaScript using:
const jsonData = ‘{“timestamp”: “2025-03-01T12:34:56.789123”}’;
const parsedData = JSON.parse(jsonData);
const dateObject = new Date(parsedData.timestamp);
console.log(dateObject); // Sat Mar 01 2025 12:34:56 GMT+0000 (UTC)
Differences Between Python and JavaScript Datetime Handling
Feature | Python (datetime) | JavaScript (Date) |
Default Format | Not JSON serializable | ISO 8601 (String) |
Conversion Needed | .isoformat() | new Date() parsing |
Timezone Handling | Supports UTC & offsets | Local timezone by default |
Want to explore JavaScript in-depth? Do register for GUVI’s JavaScript self-paced course 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.
Conclusion
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.
FAQs
Python’s json module doesn’t support datetime objects natively. You need to convert them to a string using .isoformat() before serialization.
Use .isoformat() with timezone-aware datetime objects:
from datetime import datetime, timezone
dt = datetime.now(timezone.utc)
json.dumps({“timestamp”: dt.isoformat()})
Use .toISOString():
const now = new Date();
console.log(now.toISOString());
This ensures compatibility with Python and other systems.
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.
Did you enjoy this article?