Link to home
Start Free TrialLog in
Avatar of dmontgom
dmontgom

asked on

Python - convert string to datetime

Hi,

I have two strings that I want to convert into date and datetimes...
1) 05/15/2009
2) 20 May 2009 23:09:31 -0700

I am going the route of using datetime.datetime.fromtimestamp(time.mktime(time.strptime(dd, "%d %m %Y %H:%M:%S")))

please write the two lines of code that I need for each string I want to convert..

Thanks
Avatar of HonorGod
HonorGod
Flag of United States of America image

Something like this perhaps.

I don't see anything that allows/supports the -700 though
datetime.datetime.fromtimestamp( time.mktime( time.strptime( '05/15/2009', '%m/%d/%Y' ) ) )
 
datetime.datetime.fromtimestamp( time.mktime( time.strptime( '20 May 2009 23:09:31 -0700', '%d %b %Y %H:%M%S ' ) ) )

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dmontgom
dmontgom

ASKER

What if the -0700 Is dymanic?  If it is not always -0700?
Then you would have to remove it...

That part of a date stamp is the local offset from GMT, so should stay the same, unless you are moving your machine.

One way to remove it would be shown below.

Thanks for the grade & points.

Good luck & have a great day
>>> ds = '20 May 2009 23:09:31 -0700'
>>> ds = ' '.join( ds.split( ' ' )[ 0:-1 ] )
>>> ds
'20 May 2009 23:09:31'
>>>

Open in new window