DatePart From String

Robert Rawlins - Think Blue robert.rawlins at
Tue Jul 3 12:34:17 CEST 2007

Hello Dave,

Thanks for getting back to me, I had been trying to use it like this:

time.strptime('2007-01-01 00:00:00','%H:%M:%S')

I was getting an error thrown at me: ValueError: time data did not match format:  data=2007-01-01 00:00:00  fmt=%H:%M:%S

I see how your solution works, but I'm hoping to trip is down a little bit as this has to be used in a conditional statement such as the one below:

	If time.strftime('%H:%M:%S') > time.strptime('2007-01-01 00:00:00','%H:%M:%S')
		Print 'Later In The Day'

You see how that works? I'm basically trying to check if the current time is later that the one defined in my string.

Are you able to give me a working example of how I might do this with my conditional?

Thanks Dave,


-----Original Message-----
From: at [ at] On Behalf Of Dave
Sent: 03 July 2007 10:46
To: python-list at
Subject: Re: DatePart From String

Robert Rawlins - Think Blue <robert.rawlins <at>> writes:

> I’ve tried using the time.strptime() function without much success so
> thought I’d come and ask your advice.

How exactly does it not work?? This works for me:

from time import mktime, strptime
from datetime import datetime

datetime_string = '2007-02-01 00:00:00'
datetime_object = datetime.fromtimestamp(
                  mktime(strptime(datetime_string,'%Y-%m-%d %H:%M:%S')))

print datetime_object.year
print datetime_object.month
print datetime_object.hour
print datetime_object.minute
print datetime_object.second



More information about the Python-list mailing list