python - Conversion chaîne en datetime

Mots clés : pythondatetimepython

meilleur 5 Réponses python - Conversion chaîne en datetime

vote vote

92

from datetime import datetime  datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p') 
vote vote

81

from dateutil import parser parser.parse("Aug 28 1999 12:00AM")  # datetime.datetime(1999, 8, 28, 0, 0) 
pip install python-dateutil 
vote vote

78

$ python >>> import time >>> my_time = time.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p') time.struct_time(tm_year=2005, tm_mon=6, tm_mday=1,                  tm_hour=13, tm_min=33, tm_sec=0,                  tm_wday=2, tm_yday=152, tm_isdst=-1)  timestamp = time.mktime(my_time) # convert time object to datetime from datetime import datetime my_datetime = datetime.fromtimestamp(timestamp) # convert time object to date from datetime import date my_date = date.fromtimestamp(timestamp) 
vote vote

66

from datetime import datetime  date_string = "2012-12-12 10:10:10" print (datetime.fromisoformat(date_string)) 2012-12-12 10:10:10 
vote vote

58

>>> import timestring >>> timestring.Date('monday, aug 15th 2015 at 8:40 pm') <timestring.Date 2015-08-15 20:40:00 4491909392> >>> timestring.Date('monday, aug 15th 2015 at 8:40 pm').date datetime.datetime(2015, 8, 15, 20, 40) >>> timestring.Range('next week') <timestring.Range From 03/10/14 00:00:00 to 03/03/14 00:00:00 4496004880> >>> (timestring.Range('next week').start.date, timestring.Range('next week').end.date) (datetime.datetime(2014, 3, 10, 0, 0), datetime.datetime(2014, 3, 14, 0, 0)) 

Questions similaires