
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How to convert Python date in JSON format?
There is no standard JSON format for dates. Although JavaScript does have a standard date format that is human readable, sorts correctly, includes fractional seconds(which can help re-establish chronology) and conforms to ISO 8601. You can convert a Python date to the JS date format using the strftime function and deserialize it using the client that needs this date. To get an ISO 8601 date in string format in Python 3, you can simply use the isoformat function. It returns the date in the ISO 8601 format. For example, if you give it the date 31/12/2017, it'll give you the string '2017-12-31T00:00:00'. You can use it as follows −
Example
from datetime import datetime my_date = datetime.now() print(my_date.isoformat())
Output
This will give the output −
2018-01-02T22:08:12.510696
In older python versions, you can use the strftime function to format the datetime object such that you get the desired result.
Example
from datetime import datetime my_date = datetime.now() print(my_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
Output
This will give the output −
2018-01-02T22:10:05.284208
- Related Articles
- How to convert Python date format to 10-digit date format for mysql?
- How to convert a date format in MySQL?
- Convert JSON to another JSON format with recursion JavaScript
- How to convert US date format to MySQL format in INSERT query?
- How can I convert a bytes array into JSON format in Python?
- How to convert milliseconds to date format in Android?
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- Convert MySQL Unix-Timestamp format to date format?
- How to convert JSON object to Hashtable format using PowerShell?
- How to Convert yyyymmdd to Normal Date Format in Excel?
- How to correctly convert a date format into a MySQL date?
- How to convert the hashtable to the JSON format using PowerShell?
- How to print Python dictionary into JSON format?
- How to convert milliseconds to date format in Android using Kotlin?
- Convert VARCHAR data to MySQL date format?
