Countdown d, h, min, sec and ms?

Hello @Teljemo,

See my post:
Difference between two times (measuring time)

states:

java.lang.System.currentTimeMillis() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC(coordinated universal time).

I used June 1, 2020 as my target date:
Days between Dates

Some code I wrote quickly using System.currentTimeMillis():

// Time Elapsed using System.currentTimeMillis()
// v1.0.0
// GLV 2020-02-26

import java.time.*;

long UTC = 5*60*60;  //UTC adjustment

//Target time (in seconds) was extracted from here:
//https://www.timeanddate.com/date/durationresult.html?m1=1&d1=1&y1=1970&m2=6&d2=1&y2=2020
// This could be coded if you are so inclined.

//-60*60 was DST adjustment
long targetTime = 1590969600 + UTC -1*60*60;      //2020-06-01

void setup() 
	{
  size(100, 100);
	}

void draw() 
	{
  background(0);  
  
  long timeRem = targetTime - System.currentTimeMillis()/1000;
  
  long days  = timeRem/(60*60*24);
  long hrs   = timeRem/(60*60);
  long mins  = timeRem/(60);
  long secs  = timeRem;
  long ms    = 1000 - (secs*1000 - (targetTime*1000 - System.currentTimeMillis()));
  
  println(days + ":" +  (hrs - days*24) + ":" + (mins - hrs*60) + ":" + (secs - mins*60) + ":" + ms);  
	}

My code above seems to be in sync with:

With the default frameRate of 60 fps you will only be updating in 16 ms steps.

At present it only prints to console.

This was just an exercise for me and can definitely be refined.

Update:

I added a few lines to display on canvas:
image

I used the formatting (@Chrisir also borrowed) from my code (linked above).
It should be easy to integrate into your code:
String timeString = ( nf(int(days), 3) + ':' + nf(int(hrs - days*24), 2) + ':' + nf(int(mins - hrs*60)) + ':' + nf(int(secs - mins*60), 2) + ':' + nf(int(ms), 3) );

:)

3 Likes