0

Trying to parse time from string, but get this error. Tried few formatting string.

Error:

time data '10/2/2010 0:00:00' does not match format '"%m/%d/%Y %H:%M:%S"'

code:

strdt = '10/2/2010 0:00:00'
dt = datetime.strptime(strdt, '"%m/%d/%Y %H:%M:%S"')
3
  • You must use two numbers in hours. docs.python.org/library/… Commented Sep 13, 2012 at 21:15
  • 2
    To be fair @MetalRain, when someone is trying to parse a date-time string they normally don't have much choice in how it is formatted. Commented Sep 13, 2012 at 21:16
  • @MetalRain it appears to be more forgiving in practice, I tested it just fine. It might depend on your platform though. Commented Sep 13, 2012 at 21:18

3 Answers 3

8

Your format has quotes in it. Take those out.

dt = datetime.strptime(strdt, '%m/%d/%Y %H:%M:%S')
Sign up to request clarification or add additional context in comments.

1 Comment

And to verify this: If you set strdt = '"10/2/2010 0:00:00"' the original version works and this one doesn't. As the documentation says, every non-format character is treated as a literal that must be matched exactly, so those quotes are required to match exactly.
2

try to remove quotes from format '"%m/%d/%Y %H:%M:%S"' -> '%m/%d/%Y %H:%M:%S'

Comments

1

Or you could use dateutil:

In [68]: import dateutil.parser as parser

In [69]: parser.parse('10/2/2010 0:00:00')
Out[69]: datetime.datetime(2010, 10, 2, 0, 0)

Note that by default, parser.parse interprets 10/2/2010 as being in MM/D/YYYY format.

If your string has the day before the month, then use

parser.parse("10/2/2010", dayfirst = True)

There is also the yearfirst option; see the docs for more details.

4 Comments

How does dateutil resolve m/d vs. d/m? m/d is pretty universal in the US while d/m seems to be the standard everywhere else.
@MarkRansom: From the docs "Whenever an ambiguous date is found, the dayfirst and yearfirst parameters will control how the information is processed." The default is dayfirst=False, meaning that "MM-DD-YYYY format will have precedence over DD-MM-YYYY in an ambiguous date." I think it is important to call this detail out in the answer because it will surprise non-US developers.
@MarkRansom: Except in many Asian countries, where dates are usually m/d because the overall format is y/m/d…
dateutil.parse with not dayfirst and not yearfirst isn't quite the same as m/d/y, especially if you can't trust your data (which seems plausible given the string the OP is trying to parse). In particular, if you get something like '13/6/2010' in your input, you probably want an error, but instead you're going to get June 13th. On the other hand, you might want June 13th, because you're using it as part of a very forgiving GUI interface, in which case dateutil is better. The OP should choose based on which one does what you want, not which one he can get working first.

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.