# Countdown d, h, min, sec and ms?

Hi

I googled this a fair amount now and cant really find a answer.
Can someone point me in the right direction here

Iâ€™ve just got a date for when we will have a visit from the man who owns our company.
We have a project that is suppose to be done by thenâ€¦

Would be cool to have some visuals here. A countdown timer that count days, hours, minutes, seconds and of course milliseconds to get that action.

When look around I can find a lot of count down timers but not really what im looking for.
I need this timer to count against a set target so when ever I start the program I get the correct time left.
The only thing close to this Ive found so far was a code that saved a file every day and had a database with a year of dates to keep track on where the target date is.

Is this the way to do it? I havenâ€™t really cracked the database code jetâ€¦
Im afraid there is a simple way and that I just missed itâ€¦ So Iâ€™ll better check with you first

Oh yeahâ€¦ The timer should be drawn

2 Likes

Yeah, when date and time is fixed just calc the difference to now and display this.

I wouldnâ€™t do milliseconds though

I can take a look tonight

2 Likes

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.

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

String time_ItoS(int time) {
String timeString = nf(time/3600, 2) + ':' + nf((time%3600)/60, 2) + ':' + nf(time%60, 2);
return timeString;
}
``````
3 Likes

Hello @Teljemo,

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

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.

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:
https://www.timeanddate.com/countdown/generic?iso=20200601T00&p0=1181&font=cursive

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:

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

Hey
Thx guys!!..

Something with me and timers latelyâ€¦ I have had Chrisir helping me A LOT and this type of counter, calculating difference between two times, is something that has been involved in that project also. Iâ€™m just not at that level that I can look at the huge code Chrisir has been writing and understand it

It helps a lot with these examples you show me.
I havenâ€™t really been using strings so I dont get this to work right away but I guess strings is my next step then

I will have a go on both of these codes. I guess will learn something on the way
In the first example by Chrisir there is some math to get the dates. I will look at how the time formula is made and use that to get datesâ€¦ Lets see how that works out.

In the second example by glv.
I dont get whats going on, cause it looks like I could just calculate the due date I need on the site linked to and type in the seconds where they are nowâ€¦
But when I do the timer gets a minus value and start counting upâ€¦
The date Im trying is may 26- 2020, time 09:00
Dont know whats going on hereâ€¦

Maybe something more in the background needs to be changed cause of the imort?

Howeverâ€¦ Awesome examples

3 Likes

nf(â€¦, 2) just makes a leading zero for second and months and so on

so itâ€™s 2020/09/15 and not 2020/9/15

same with time

glvâ€™s code is much better though

2 Likes

Hi,

Provide me with a target date and your location (have to adjust for UTC) and I will guide you to update your code.

This code was something I threw together as an exercise to use the System.currentTimeMillis(); it seems to work and I am using it as a countdown timer to my vacation.

The line that converts it to a string is just to make it easy to text() to the canvas; look up strings and the references to understand this.

`:)`

thatâ€™s what he wrote

Itâ€™s European Summer time afaik - CEST

1 Like

Thank you for the kind words; I really wanted to use System.currentTimeMillis() and was able to sort this out and had fun doing it.

This was only a quick first pass at this and those can always be finessed; my first pass at code always shows the thought processes and steps and still worthy of posting.

I will certainly be looking at this again when I have time.
It is counting down to my vacation!

`:)`

1 Like

@Teljemo

Please run this and tell me what you are getting:

``````println(System.currentTimeMillis());
``````

I get:

And also the exact time from your location.

`:)`

This code will work in Java mode and will work and takes into account local time zone for you. I donâ€™t think it will work in JS mode, you would need to find an equivalent class as GregorianCalendar.

I have left the code and variable names verbose to make the semantics clearer. I am sure someone will waste time trimming down the variables names and removing some blank spaces LOL.

``````import java.util.*;

int a_year = 2020;
int a_month = Calendar.JUNE; // or use numbers where 0 = Jan : 1 = Feb ...
int a_day_of_month = 6;
int a_hour = 13;   // 0 - 23
int a_minute = 50; // 0 - 59
int a_second = 45; // 0 - 59

long ms_in_second = 1000;
long ms_in_minute = 60 * ms_in_second;
long ms_in_hour = 60 * ms_in_minute;
long ms_in_day = 24 * ms_in_hour;

int days, hours, minutes, seconds, milliseconds;

long arrives_at;

public void setup() {
size(600, 300);
GregorianCalendar boss_arrives =  new GregorianCalendar(a_year, a_month, a_day_of_month
, a_hour, a_minute, a_second);
arrives_at = boss_arrives.getTimeInMillis();

textSize(24);
textAlign(CENTER, CENTER);
}

void draw() {
background(0);
fill(255);
long n =  arrives_at - System.currentTimeMillis();
days = (int) (n / ms_in_day);
n -= days * ms_in_day;
hours = (int) (n / ms_in_hour);
n -= hours * ms_in_hour;
minutes = (int) (n / ms_in_minute);
n -= minutes * ms_in_minute;
seconds = (int) (n / ms_in_second);
n -= seconds * ms_in_second;
milliseconds = (int) n;

text("DAYS", 20, 30, 100, 50);
text("HRS", 120, 30, 100, 50);
text("MINS", 220, 30, 100, 50);
text("SECS", 320, 30, 100, 50);
text("MS", 420, 30, 100, 50);

text("" + days, 20, 60, 100, 50);
text("" + hours, 120, 60, 100, 50);
text("" + minutes, 220, 60, 100, 50);
text("" + seconds, 320, 60, 100, 50);
text("" + milliseconds, 420, 60, 100, 50);
}
``````
1 Like

Hey Chris,

did you find a solution for the date difference meanwhile?

I could work great with your code, unfortunately I need to fix the problem you already mentioned for my project:

â€ś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.â€ť

You would totally made my day if you did

The Solution by glv is much better

Hey and welcome to the forum!

Great to have you here!

1 Like

I updated my previous code above for New Years!
Countdown d, h, min, sec and ms?

It still appears to sync (for testing) with this site:
https://www.timeanddate.com/countdown/newyear

Countdown to New Year
``````// Time Elapsed using System.currentTimeMillis()
// v1.0.1
// GLV 2020-12-20

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.

// For Jan 1, 2021
// https://www.timeanddate.com/date/durationresult.html?m1=1&d1=1&y1=1970&m2=1&d2=1&y2=2021

long targetTime = 1609459200   + UTC;      //2021-01-01

void setup()
{
size(950, 200);
}

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);
}
``````

The string is in previous post to display text to sketch window.

I already have code to enter targetime directly instead of looking it up. I donâ€™t post everythingâ€¦

`:)`

2 Likes