# Difference between two times (measuring time)

**URL:** <https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728>\
**Category:** Coding Questions\
**Created:** [November 22, 2019, 11:42pm UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728 "2019-11-22T23:42:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 22, 2019, 11:42pm UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/1 "2019-11-22T23:42:10Z")

</div>

Hello all,

[Edited]

let’s say I have times like 00:36:04 and 03:16:05 as Strings (which is an actual time and not a duration).

How can I have the difference (duration) between the two times like “4h, 23min, 13sec”  
or “03:20:01”?

I searched everything like here [https://docs.oracle.com/javase/7/docs/api/index.html?java/text/StringCharacterIterator.html](https://docs.oracle.com/javase/7/docs/api/index.html?java/text/StringCharacterIterator.html) but with no avail

Something like (pseudo code, I am making this up):

```auto
  DateFormat df = DateFormat.getDateInstance();
  time1= df.format("00:36:04") ; 
  time2= df.format("03:16:05") ; 
  time3=time2-time1; 
  String result = time3.toString();

```

thanks everyone

Chrisir

(this is coming from here by the way: [Can I make a small windows app for taking time and log production times?](https://discourse.processing.org/t/can-i-make-a-small-windows-app-for-taking-time-and-log-production-times/15484/21))

---

<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:** [November 23, 2019, 12:41am UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/2 "2019-11-23T00:41:44Z")

</div>

- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#parse(java.lang.CharSequence)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#parse(java.lang.CharSequence))
- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#minus(java.time.Duration)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#minus(java.time.Duration))
- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#toMillis()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/time/Duration.html#toMillis())

```auto
// Discourse.Processing.org/t/difference-between-two-times-measuring-time/15728/2
// GoToLoop (2019/Nov/22)

import java.time.Duration;

final String PT = "PT";

final Duration
  time1 = Duration.parse(PT + "36m4s"), 
  time2 = Duration.parse(PT + "3h16m5s");

println(time1, time1.toMillis() / 1000);
println(time2, time2.toMillis() / 1000);

final Duration time3 = time2.minus(time1);
println(time3, time3.toMillis() / 1000);

exit();

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 24, 2019, 2:38am UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/3 "2019-11-24T02:38:16Z")

</div>

This was my programming exercise this morning:

```auto
String time1 = "00:36:04"; 
String time2 = "03:16:08"; 

int hour;
int min;
int sec;

int start;
int stop;
int elapsed;

void setup() 
	{
  size(300, 220);
  // Current time as String
  //time1 = nf(hour(), 2) + ':' + nf(minute(), 2) + ':' + nf(second(), 2);
  textSize(48);
  }
  
void draw() 
	{
  background(0);
  translate(width/2, height/2);
  
  //println(time1);    
  //convert hour + min + sec to integer
  start = time_StoI(time1);
 
  //Stop time as String
  //time2 = nf(hour(), 2) + ':' + nf(minute(), 2) + ':' + nf(second(), 2);
  
  //println(time2);
  //convert hour + min + sec to integer
  stop = time_StoI(time2);
  
  elapsed = stop - start;
  
  textAlign(CENTER, CENTER);
  text(time_ItoS(start), 0, -60-10);
  text(time_ItoS(stop), 0, 0-10);
  text(time_ItoS(elapsed), 0, 60-10);
	}

int time_StoI(String time)
  {
  hour = (int(time.charAt(0))-0x30)*10 + int((time.charAt(1))-0x30);
  min = (int(time.charAt(3))-0x30)*10 + int((time.charAt(4))-0x30);
  sec = (int(time.charAt(6))-0x30)*10 + int((time.charAt(7))-0x30);  
  int total = hour*60*60 + min*60 + sec;     
  return total;
  }
  
String time_ItoS(int time)
  {
  String timeString = nf(time/3600, 2) + ':' + nf((time%3600)/60, 2) + ':' + nf(time%60, 2);
  return timeString;
  }

```

You can uncomment time1 and time2 to show time elapsed from start of program.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d3ff15f6b0e07cdafaf1c420a4e648ee22fb61d2.png)

[Days Calculator: Days Between Two Dates](https://www.timeanddate.com/date/durationresult.html?m1=11&d1=24&y1=2019&m2=11&d2=24&y2=2019&h1=0&i1=36&s1=4&h2=3&i2=16&s2=8)

🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 24, 2019, 1:05pm UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/4 "2019-11-24T13:05:18Z")

</div>

I made a very similar sketch a year ago, although i used Date to save the current time and add some time to it, to know when i could perform an action the next time. (Like taking medicine every 4h or to stop smoking [with increasing intervals])

It worked quite well, while i used it 😅 If someones interested, i‘ll try to find the Code, but it’s pretty easy… just convert Date to int and add a value in milliseconds to in/decrease it.

Just had to comment cause the image is sooo similar 😅

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [November 24, 2019, 7:48pm UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/5 "2019-11-24T19:48:18Z")

</div>

Wanted to make a timer without millis() when I was a beginner in processing. Came up with an interesting design a while later, no idea if it would work implemented in other programs. Just sharing.

```auto
float s=0;
float w=0.0152;

void draw() {
background(204);
s+=w;
fill(0);
textSize(30);
text(floor(s),200,100);}

```

---

<div class="post-metadata">

**Author:** ![unique\_username](https://avatars.discourse-cdn.com/v4/letter/u/a88e57/32.png) [@unique\_username](https://discourse.processing.org/u/unique_username)\
**Post date:** [November 24, 2019, 7:51pm UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/6 "2019-11-24T19:51:09Z")

</div>

EDIT;  
May have to experiment with _w_ value, it is not exact.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 25, 2019, 1:30am UTC](https://discourse.processing.org/t/difference-between-two-times-measuring-time/15728/7 "2019-11-25T01:30:21Z")

</div>

Hello,

There are many ways to do this.

My example was focused on working with hh:mm:ss String format in first post.

I learn something new with each exercise.

🙂
