Page 1 of 1

Help with datetime.strftime

Posted: Fri Feb 02, 2018 10:15 am
by ricardo.santos
Don't be to hard on me. I now this should be basic. I'm a newbie

I want to calculate a duration between a starting date and current date.
My starting date is in a dictionary (as string)

why my code doen't run?

Code: Select all

from datetime import datetime

init_data = {'date': '2015-08-30','time': '15:35:20'}

init_date_time = init_data['date'] + ' ' + init_data['time']
init_date_time_f = datetime.strftime(init_date_time, '%Y-%m-%d %H:%M:%S')

duration = datetime.now() - init_date_time_f

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 10:48 am
by fredoll
It would be easier to help you if you give the error that you see ;-)

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 10:55 am
by B.Goode
I want to calculate a duration between a starting date and current date.
My starting date is in a dictionary (as string)
I genuinely don't know, but you could check by reading the documentation for the library: can the datetime module do date arithmetic on quantities that are stored as strings?

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 11:09 am
by ricardo.santos
the code error is

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

the problem is the conversion of string to date time (function strftime)

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 11:11 am
by ricardo.santos
WTF

the problem is that the function is strptime and not strftime
dear god!!!!

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 11:49 am
by PingoBags
strptime converts string to a datetime object
strftime converts datetime object to string

Code: Select all

from datetime import datetime

DTNow = datetime.now().replace(microsecond=0)
DTThen = datetime.strptime('2015-08-30 15:35:20', '%Y-%m-%d %H:%M:%S') 
   
print(DTNow - DTThen)
or using your code

Code: Select all

from datetime import datetime

init_data = {'date': '2015-08-30','time': '15:35:20'}
init_date_time = init_data['date'] + ' ' + init_data['time']
DTNow = datetime.now().replace(microsecond=0)
DTThen = datetime.strptime(init_date_time, '%Y-%m-%d %H:%M:%S') 
   
print(DTNow - DTThen)

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 12:14 pm
by scotty101
Based on PingoBags's answer, there is an alternative way to do it that doesn't involve joining the date and time together to a string first.

Code: Select all

from datetime import datetime

init_data = {'date': '2015-08-30','time': '15:35:20'}

myDate = datetime.strptime(init_data['date'],'%Y-%m-%d').date()      #Get a date object
myTime = datetime.strptime(init_data['time'],'%H:%M:%S').time()      #Get a time object
myDateTime = datetime.combine(myDate,myTime)                         #Combine time and data objects to create a datetime object

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 2:03 pm
by scruss
Or another way to do it (as There Is More That One Way To Do It), with more 'Do What I Mean':

Code: Select all

from datetime import datetime
import dateutil.parser

init_data = {'date': '2015-08-30','time': '15:35:20'}
init_date_time = init_data['date'] + ' ' + init_data['time']

duration = datetime.now() - dateutil.parser.parse(init_date_time)
You'll need to install dateutil, which is in the repos

Code: Select all

sudo apt install python3-dateutil python-dateutil
Parsing dates is always a bit hacky, but it's easy until you meet time zones and Americans ...

Re: Help with datetime.strftime

Posted: Fri Feb 02, 2018 4:37 pm
by ricardo.santos
many thanks.
Now i have plenty of options