ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Help with datetime.strftime

Fri Feb 02, 2018 10:15 am

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

fredoll
Posts: 227
Joined: Thu Jan 10, 2013 10:30 am

Re: Help with datetime.strftime

Fri Feb 02, 2018 10:48 am

It would be easier to help you if you give the error that you see ;-)

User avatar
B.Goode
Posts: 8252
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Help with datetime.strftime

Fri Feb 02, 2018 10:55 am

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?

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: Help with datetime.strftime

Fri Feb 02, 2018 11:09 am

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)

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: Help with datetime.strftime

Fri Feb 02, 2018 11:11 am

WTF

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

PingoBags
Posts: 47
Joined: Mon Nov 06, 2017 10:53 pm
Location: NS, Canada

Re: Help with datetime.strftime

Fri Feb 02, 2018 11:49 am

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)

scotty101
Posts: 3674
Joined: Fri Jun 08, 2012 6:03 pm

Re: Help with datetime.strftime

Fri Feb 02, 2018 12:14 pm

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
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
scruss
Posts: 2419
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Help with datetime.strftime

Fri Feb 02, 2018 2:03 pm

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 ...
Last edited by scruss on Sat Feb 03, 2018 3:02 pm, edited 1 time in total.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: Help with datetime.strftime

Fri Feb 02, 2018 4:37 pm

many thanks.
Now i have plenty of options

Return to “Python”