1

I'm working on an image upload utility, and part of the functionality is to parse the IPTC and EXIF data of the images.

IPTCInfo gets the information I need, but the date fields are in the format 20130925.

Now, I can break that integer up into 2013 09 25 and create a date object. Before I do so, is there already existing functionality to solve this issue?

1
  • Why do I get the feeling that in 7986 years they're going to drag me out of cryonic retirement to fix your y10k problem? :)
    – abarnert
    Commented Oct 3, 2013 at 1:29

2 Answers 2

8

The date class doesn't have a string-parsing function, but the datetime class does, strptime.

So, first make a datetime, then extract the date part of it:

>>> s = '20130925'
>>> dt = datetime.datetime.strptime(s, '%Y%m%d')
>>> d = dt.date()
>>> d
datetime.date(2013, 9, 25)

If you don't understand where the '%Y%m%d' comes from, see strftime() and strptime() Behavior.

1

You can use datetime.strptime:

>>> import datetime
>>> datetime.datetime.strptime("20130925","%Y%m%d").date()
datetime.date(2013, 9, 25)
2
  • Except he asked for a date object, not a datetime.
    – abarnert
    Commented Oct 3, 2013 at 1:31
  • Oops, you're right @abarnert. I edited my answer to return a date object.
    – space
    Commented Oct 3, 2013 at 9:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.