Hello,
I would like to calculate elapsed time based on start and end times (extracted from an XML file)
startDate=“2016-11-16 09:26:33 +1000” endDate="2016-11-16 09:26:33
As the times are embedded in a string(see above),
I think
firstly, I need to extract the start and end times into hour, minute, seconds variables from the string
for example., hour from 12th position, length 2 (please help with Python syntax to do this, thanks)
I’m not sure what the “+1000” part is for? Anyhow, here’s some code that should do it for you:
from datetime import datetime
startDate = '2016-11-16 09:26:33 +1000'
endDate = '2016-11-16 09:26:33'
# strip-off the +1000 part
startPlus = startDate[startDate.index('+')+1:] # 1000
startDate = startDate[:-len(startPlus)-2] # 2016-11-16 09:26:33
# convert start date to datetime format
startDate = datetime.strptime(startDate, '%Y-%m-%d %H:%M:%S')
# convert end date to datetime format
endDate = datetime.strptime(endDate, '%Y-%m-%d %H:%M:%S')
# difference is zero seconds, but you can fiddle with the initial variables
print( (startDate-endDate).total_seconds() )
I’ve stripped-off the +1000 part, but stored the 1000 in the startPlus variable if you need it.
Thank you for your replies with coding plus attachments (extra info.) @tabreturn@GoToLoop
all very useful and contribute to the progress of my program.
@GoToLoop@tabreturn
Thank you for the above code (a while ago) for using the datetime routines, working well for my sketches written using Processing Python mode, .
Instead of Python mode, I now want to do the same coding in Processing (version 3.5.4) but unsure of the syntax to import datetime and subsequent calls to strip dates, as above.
For example, when I replaced ‘from datetime import datetime’ in Python mode
with ‘import datetime’ or ‘import datetime.*’ in Processing mode,
I get an error message: The package “datetime” does not exist. You might be missing a library…no Library found for datetime
Any tips and sample code in Processing appreciated, thanks.
Thanks for above tips but need a bit more help.
So I am trying to calculate using Processing code, the difference in seconds between two date-time stamps (format "yyyymmdd hh:mm:ss).
For example, “2016-01-01 00:01:02” minus “2015-12-31 23:59:10” should return 112 seconds.
Much thanks in advance for further tips.
@tabreturn and @GoToLoop, belated thanks to your suggestions a while ago. So what I did (for those who may find it useful) to calculate difference in seconds between two date-time stamps (using hints/suggestions above by @tabreturn)