DatePart From String

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Tue Jul 3 06:34:17 EDT 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,

Rob

-----Original Message-----
From: python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org [mailto:python-list-bounces+robert.rawlins=thinkbluemedia.co.uk at python.org] On Behalf Of Dave
Sent: 03 July 2007 10:46
To: python-list at python.org
Subject: Re: DatePart From String

Robert Rawlins - Think Blue <robert.rawlins <at> thinkbluemedia.co.uk> 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.day
print datetime_object.hour
print datetime_object.minute
print datetime_object.second

HTH,
Dave








-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list