# Converting millis to Sec or Hours for Uptime Display

Hello
Sorry for the basic question but wondering what the simplest way to change the display of milliseconds to minutes or hours would be.

Currently using the following to track hardware uptime
int value = millis() / 1000;
Blynk.virtualWrite(V4, value);

That displays milliseconds but its a crazy number and hard to digest visually.
id prefer to see uptime in hours & minutes if possible

How would i go about converting the milliseconds to minutes ? or hours? or both?
int value = (millis() / 1000) / 60 / 60 ; // icantzmath
Blynk.virtualWrite(V4, value);
or
int value = millis() / 1000 / 60 / 60 ; // icantzmath
Blynk.virtualWrite(V4, value);

i know in python i can use regular math to achieve results but not sure about c++
i tried searching but was unable to find.

Itâ€™s just the same with C++.

that displays seconds, actuallyâ€¦

you would better search for this time conversion topic on coding forums, like stackoverflow, iâ€™m pretty sure there are dozens of ready made solutions for this.

G GLE

Try this:

``````#include <TimeLib.h>

void TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;

}
``````

Usage is:

``````Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
``````
3 Likes

@Gunner @wanek â€¦ thanks for the inputâ€¦ as i mentioned i did search google & blynk community but was unable to understand how to expand millisec to hours, minutes secsâ€¦ which is why i posted in HELP section.

is there a section for total noobs who dont know how to code?

I use Blynk mainly because i dont have to code and because its a quick way to get from idea to reality =)

@jamin Thank you kind sirâ€¦ exactly what i was looking for.

Yesâ€¦ but I am guessing you will not like the answer Google for programming how-to websites.

That is true, it can be used that wayâ€¦ but it is also very limited to what Blynk is actually capable of. Trust me, I was the same wayâ€¦ but a little (OK lot of ) reading and learning later and I can do MUCH more than twiddle a few controls and blink a few LEDâ€™s.

1 Like

@jamin sorry for multiple posts but im getting an error

`````` error: a function-definition is not allowed here before '{' token
void TimeShowFormatted(int ms) {
: error: expected '}' at end of input
}
``````
``````BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
void TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}
``````

what am i doing wrong?

Format your code correctly so it makes senseâ€¦ i cant read any of that.

```cpp
// CODE HERE
```

but looks like you just need to remove the last bracket

@jamin thanksâ€¦ that formatting job was terrible. its fixed now.
I tried removing the last bracket after Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
but still getting that error

``````BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
void TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}
``````

This is not how to use this commandâ€¦ It is not a function in of itself (e.g. surrounded in the brackets)â€¦ but it does need to be put into a proper function (e.g. `void DoSomethingHere()` ) in order to run properly.

This is where all that pesky programming need to know kicks inâ€¦

Check out some of the examples in the Sketch Builder to see how these commands work.

@Gunner
i did try that but get the same error

`````` In function 'void BlynkOnConnected()':
v2v3:26: error: a function-definition is not allowed here before '{' token
void TimeShowFormatted(int ms) {
: error: expected '}' at end of input
}
``````
``````BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
void TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}
``````

If these three lines are in your codeâ€¦ and it looks like all you did was copy/paste @Jaminâ€™s code(s) but lumped them all togetherâ€¦ then thatâ€™s your problem. This is one reason we donâ€™t encourage just asking for code, or offering code to those who donâ€™t want to learn codeâ€¦ because if you donâ€™t know how to properly set it up, it is uselessâ€¦

Take a look at those links I showed you, figure out how you want to display that time via that `Blynk.virtualWrite(V4, TimeShowFormatted(millis()));` command and create a proper `void function` for it (preferably using a timer else you may flood your program).

I donâ€™t know what hardware you are using, but look at this ESP8266 example and see how the `void myTimerEvent()` is setupâ€¦

PS no need to keep tossing @Jaminâ€™s code examples back at usâ€¦ we already see it above

``````BLYNK_CONNECTED() {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
Blynk.virtualWrite(V4, TimeShowFormatted(millis()));
}

void TimeShowFormatted(int ms) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = ms / 1000; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
return days + hours_o + hours + mins_o + mins + secs_o + secs;
}
``````
3 Likes