Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Getting the date format [duplicate]

I'm getting a date as a string, then I'm parsing it to datetime object. Is there any way to check what's is the date format of the object?

Let's say that this is the object that I'm creating:

modified_date = parser.parse("2015-09-01T12:34:15.601+03:00")

How can i print or get the exact date format of this object, i need this in order to verify that it's in the correct format, so I'll be able to to make a diff of today's date and the given date.

like image 291
Alex Brodov Avatar asked Jan 08 '23 02:01

Alex Brodov


1 Answers

I had a look in the source code and, unfortunately, python-dateutil doesn't expose the format. In fact it doesn't even generate a guess for the format at all, it just goes ahead and parses - the code is like a big nested spaghetti of conditionals.

You could have a look at dateinfer which looks to be what you're searching for, but these are unrelated libraries so there is no guarantee at all that python-dateutil will parse with the same format that dateinfer suggests.

>>> from dateinfer import infer
>>> s = "2015-09-01T12:34:15.601+03:00"
>>> infer([s])
'%Y-%d-%mT%I:%M:%S.601+%m:%d'

Look at that .601. Close but not cigar. I think it has probably also mixed up the month and the day. You might get better results by giving it more than one date string to base the guess upon.

like image 132
wim Avatar answered Jan 19 '23 08:01

wim