Countdown d, h, min, sec and ms?

here is an example.

Afaik there is now way to get the millis() from the computer as simple as you get the seconds().

(The command millis() doesn’t give you the millis() since the last second started but since the program started. I mean you could calculate the millisOfTheCurrentSecond roughly, but why?)

Also, you need to figure the date difference. Not fully trivial, because months are of different lengths (and years also, every 4 years, e.g. 2020). There are probably inbuilt functions (see gotoloop comment in the discussion in the link below).

I did the time difference only (with help from the forum)

Also, it wouldn’t work when it’s 11 o’clock a.m. and the guy arrives in 3 days at 9 o’clock a.m. I guess.

:wink:

See Difference between two times (measuring time)

Chrisir

String dateGuest = "29/02/2020";  // change this
String timeGuest = "23:45:00";


void setup() {
  size(600, 600);
}//func 

void draw() {
  background(0); 
  text("Now "+getDate() + "    " + getTime(), 100, 100);
  text("Guest arrives at "+dateGuest + "    " + timeGuest, 100, 130);

  String dateNow = getDate(); 
  String timeNow = getTime();

  String timeDiff = timeDifference(timeNow, timeGuest);
  String dateDiff = "???";
  text("Diff    "+timeDiff + "    " + dateDiff, 100, 160);
}//func

// -----------------------------------------------------------------------

String getDate() {
  return 
    nf(day(), 2)
    +"/"
    +nf(+month(), 2)
    +"/"
    +nf(year(), 4);
}

String getTime() {
  return
    nf(hour(), 2) 
    +":"
    +nf(minute(), 2)
    +":"
    +nf(second(), 2);
}

String timeDifference(String t1, String t2) {
  //convert hour + min + sec to integer
  int start = time_StoI(t1);
  //convert hour + min + sec to integer
  int stop = time_StoI(t2);
  //difference 
  int elapsed = stop - start;

  return time_ItoS(elapsed);
}//

int time_StoI(String time) {
  // convert time String to millis (expecting 11:11:03)
  int hour;
  int min;
  int sec;

  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;
}
3 Likes