# Stripping time from a string to calculate elapsed time

**URL:** <https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503>\
**Category:** Processing.py\
**Created:** [July 4, 2019, 5:04pm UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503 "2019-07-04T17:04:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [July 4, 2019, 5:04pm UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/1 "2019-07-04T17:04:23Z")

</div>

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)
- then convert hour, minute & second into seconds
- then calculate difference in seconds

Regards, Paul

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 4, 2019, 5:28pm UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/2 "2019-07-04T17:28:13Z")

</div>

[Docs.Python.org/2.7/library/datetime.html#module-datetime](http://Docs.Python.org/2.7/library/datetime.html#module-datetime)  
[Docs.Python.org/2.7/library/datetime.html#strftime-strptime-behavior](http://Docs.Python.org/2.7/library/datetime.html#strftime-strptime-behavior)

> <https://stackoverflow.com/questions/8258432/days-between-two-dates>

```python
from datetime import datetime

a = datetime(2016, 11, 16, 9, 26, 33, 1000)
b = datetime.today()

print a, ENTER, b, ENTER, b - a
exit()

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [July 4, 2019, 9:39pm UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/3 "2019-07-04T21:39:10Z")

</div>

I’m not sure what the “+1000” part is for? Anyhow, here’s some code that should do it for you:

```auto
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.

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [July 5, 2019, 10:40am UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/4 "2019-07-05T10:40:19Z")

</div>

Thank you for your replies with coding plus attachments (extra info.)  
@tabreturn @GoToLoop  
all very useful and contribute to the progress of my program.

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [February 6, 2021, 5:54am UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/5 "2021-02-06T05:54:38Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 6, 2021, 6:17am UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/6 "2021-02-06T06:17:41Z")

</div>

> <https://stackoverflow.com/questions/13037654/subtract-two-dates-in-java/13038018#13038018>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDateTime.html)  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html)

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 6, 2021, 6:22am UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/7 "2021-02-06T06:22:22Z")

</div>

To add to @GoToLoop’s response –

The `datetime` is a Python module. You’ll have to import the necessary java modules, something like this:

```auto
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

LocalDate dateA = LocalDate.parse("2011-01-01");
LocalDate dateB = LocalDate.parse("2021-02-01");
print(ChronoUnit.DAYS.between(dateA, dateB));

```

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [December 10, 2022, 4:03pm UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/8 "2022-12-10T16:03:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![proc8888](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@proc8888](https://discourse.processing.org/u/proc8888)\
**Post date:** [December 11, 2022, 2:14am UTC](https://discourse.processing.org/t/stripping-time-from-a-string-to-calculate-elapsed-time/12503/9 "2022-12-11T02:14:58Z")

</div>

@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)

1. import java.time.LocalDateTime;  
import java.time.temporal.ChronoUnit;
2. convert strings “yyyy-mm-dd hh:mm:ss” to “yyyy-mm-ddThh:mm:ss” (by concatenating strings “yyyy-mm-dd” + “T” + “hh:mm:ss”)
3. parse these 2 strings using LocalDateTime.parse (converted\_string)
4. calculate the difference in seconds between the parsed date-time stamps using ChronoUnit.SECONDS.between(parsed date-time 1 - parsed date-time 2)
