1

I have a unicode string u'May 12, 2014 8:00:40 PM' and I want to convert it into a date time object but I dont want time stamp, only date.

output should be 2014/5/12 or 2014/05/12

I tried to use strptime but it didn't help me. may be I'm missing something else?

/usr/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
    326     if len(data_string) != found.end():
    327         raise ValueError("unconverted data remains: %s" %
--> 328                           data_string[found.end():])
    329 
    330     year = None

ValueError: unconverted data remains:   1:33PM

I've already seen these threads

Converting string into datetime

convert string to date type python

Convert string into Date type on Python

Community
  • 1
  • 1
user3628682
  • 685
  • 1
  • 11
  • 19
  • 1
    *I tried to use strptime but it didn't help me*. **How** did it not help you? What errors did you get, or how was the output not what you wanted? – Martijn Pieters May 20 '14 at 10:52
  • You've *"seen these threads"*. What, if anything, did you learn from them? What do you still want to know? Have you read [the `datetime` docs](https://docs.python.org/2/library/datetime.html)? You might find [this](http://stackoverflow.com/questions/23293697/comparing-datetime-objects-by-date-only/23293996#23293996) useful. – jonrsharpe May 20 '14 at 10:54
  • 1
    You want to slice the input string to not include the timestamp, *or* parse the time portion as well. `strptime()` expects to parse *all of the input string*. – Martijn Pieters May 20 '14 at 10:55

1 Answers1

1

datetime.datetime.strptime() expects to parse all of the input string. If you pass in u'May 12, 2014 8:00:40 PM', you'll need to account for all characters.

Either split off the time portion, or parse the time portion as well, then ignore that part by calling .date() on the resulting datetime.datetime() object.

It's easier to just include the time:

dateobj = datetime.datetime.strptime(inputstring, '%B %d, %Y %I:%M:%S %p').date()

This gives you a datetime.date() object for just the date portion of the string.

Cutting of the time portion can be done with a str.rsplit() call, limited to 2 splits:

dateobj = datetime.datetime.strptime(inputstring.rsplit(None, 2)[0], '%B %d, %Y').date()

I've called .date() here as well.

Demo:

>>> import datetime
>>> inputstring = u'May 12, 2014 8:00:40 PM'
>>> datetime.datetime.strptime(inputstring, '%B %d, %Y %I:%M:%S %p').date()
datetime.date(2014, 5, 12)
>>> datetime.datetime.strptime(inputstring.rsplit(None, 2)[0], '%B %d, %Y').date()
datetime.date(2014, 5, 12)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @user3628682: then include the comma; I've done so now. – Martijn Pieters May 20 '14 at 11:02
  • @user3628682: perhaps; here `%b` works (`May` is abbreviated to `May`), but `%B` is for full month names, `%b` for abbreviated months. Look at other examples; if you see `Mar 12, 2014` you need to use `%b`, if it uses `March 12, 2014`, you need to use `%B`. – Martijn Pieters May 20 '14 at 11:07
  • @user3628682: No. You can't delete it because it has an upvoted answer now, anyway. And there are two duplicate *votes* on your post, it hasn't yet been put on hold; others would have to agree that it is a dupe. And last but not least, duplicate posts serve as sign-posts; it your post *is* closed as a dupe, then people using search keywords that point to your post can then find a canonical post from there. – Martijn Pieters May 20 '14 at 11:12