Skip to content

Convert a JavaScript Date to a Python date object

In this post you will learn how to convert a JavaScript date format to Python date format. This conversion is needed when you are consuming APIs written in Python and you’d like to convert the date to the Python date format from JavaScript date format.

import datetime
# JavaScript date received
date_string = '2020-10-11'
date_format = '%Y-%m-%d'
try:
  date_obj = datetime.datetime.strptime(date_string, date_format)
  print(date_obj)
except ValueError:
  print("Incorrect date format, should be YYYY-MM-DD")

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.