0

I am trying to parse a date with a specific format

This is my date string 2020-08-12 00:00:00 +05:30
Format string: %Y-%m-%d %H:%M:%S %Z

Code that does the formatting:

date_object = datetime.datetime.strptime(date_string, format_str)

Output: ValueError: time data '2020-08-12 00:00:00 +05:30' does not match format '%Y-%m-%d %H:%M:%S %Z'

Things I've tried: Changing Zone from Caps to small z with format string as %Y-%m-%d %H:%M:%S %z

Python Version: 3.6

1
  • 1
    in Python 3.6's datetime, the colon between hours and minutes of the UTC offset is not supported. and it's a lower-case %z. Commented Aug 13, 2020 at 13:59

1 Answer 1

1

I have tried with lowercase z, and worked for me e.g.:

import datetime

date_string="2020-08-12 00:00:00 +05:30"
format_str="%Y-%m-%d %H:%M:%S %z"

date_object = datetime.datetime.strptime(date_string, format_str)


print('Date-time:', date_object)

prints

Date-time: 2020-08-12 00:00:00+05:30

EDIT

I'm using Python 3.7 so Sam's comment explains why it works for me.

Looking at the 3.6 datetime documentation, does removing the colon from the timezone work for you e.g. instead of

date_string="2020-08-12 00:00:00 +05:30"
date_string="2020-08-12 00:00:00 +0530"
Sign up to request clarification or add additional context in comments.

2 Comments

Welcome to Stack Overflow. I've told that I've already tried with a lowecase z and I get the same error
lowercase z changed in 3.7. maybe a package like python-dateutil would be better, or you could work around with a regex to fix things up in older versions of Python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.