Difference between two times (measuring time)

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 but with no avail

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

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

3 Likes
// 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();
5 Likes

This was my programming exercise this morning:

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

Days Calculator: Days Between Two Dates

:slight_smile:

3 Likes

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 :sweat_smile: 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 :sweat_smile:

1 Like

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.

float s=0;
float w=0.0152;

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

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

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.

:slight_smile:

2 Likes