There are two reasons I write about this. I accidentally shut off my remote access and I cannot complete the other tutorial I had in plans, and I think this is a very useful task, which I’m actually going to use in my next Tasker project.
Seconds to DD:HH:MM:SS
The system variable %TIMES gives you the number of seconds from 1 Jan 1970 and this count can be used for a lot of things in Tasker, including timers. Since humans don’t operate well with 10 digit numbers (unless you are one of these rich humans – PayPal details below!) so translating this into a readable form is most desired.
The easy mode would be to do some basic maths and call it quits with the format:
00d 00h 00m 05s, but that’s just lazy, and my reputation of “knowing something about Tasker” calls for a more sophisticated approach. How about these:
05s 32m 03s 07h 04m 02s 01d 10h 28m 01s
It makes complete sense to display the only range of the values in use. No need to use “days” if all I have is 10 min worth of seconds in my timer!
Maths behind the seconds
To convert Seconds to DD:HH:MM:SS all I need is good old math! So how to extract days, hours, minutes and seconds from a very big integer? Modulus and rounding functions are the primary answers.
Modulus (% – yes like in a variable) the modulo operation finds the remainder after division of one number by another. Or in common language – the result is the remainder of the division that doesn’t turn the result into a number with a decimal:
16%5 = (5+5+5)+1 = 1 25%7 = (7+7+7)+4 = 4
The calculation is super handy if you want to check if you are dealing with an odd or even number:
X%2 = 1 odd x%2 != 1 even
Rounding options – are the easiest ways to get rid of the decimal point. There is more than one function and consider the potential result:
floor(x) round down --> floor(2.8) = 2 ceil(x) round up --> ceil(2.8) = 3
So far, this post has been a great “back to school” warm-up! Right? Let’s brain some more!
To extract a number of days from X seconds we have to:
Xsec /24h/60m/60s and round down so: floor(x/86400)
To calculate the total number of hours:
Xsec /60m/60s and round down so: floor(x/3600) but here is an issue, I want the complete days to be excluded, so the actual formula would look like:
(Xsec%86400) / 60m/60s and round down so: floor((x%86400)/3600)
In a similar way, to get the total number of minutes one must
Xsec /60s and round down so: floor(x/60) but I don’t want to count minutes that are already counted in the hours, therefore:
(Xsec%3600) /60s and round down so: floor((x%3600)/60)
I’m including these because of my OCD, for the upcoming project, seconds will have minimal impact. Let’s do it anyway because you may need it! Since you know the total number of seconds X. All you need is to extract the ones that are under 1 min:
X%60 - done!
Use JavaScriplet action to perform all the maths calculations in a single action and extract the values you need. Set the variable %par to specify the seconds (Scriplet don’t like the %par1 )and use the code:
d = ("0"+Math.floor(par/86400)).slice(-2); h = ("0"+Math.floor((par%86400)/3600)).slice(-2); m = ("0"+Math.floor((par%3600)/60)).slice(-2); s = ("0"+par%60).slice(-2); var timer = ""; timer += (par>86400?d+"d ":""); timer += (par>3600?h+"h ":""); timer += (par>60?m+"m ":""); timer += s+"s";
The value will be available in the %timer variable. Add the return action to make it usable as a subtask. If you going to use this JavaScriplet, you can jump to the conclusion.
If you are just getting started with Tasker, don’t panic. This task is the 3rd iteration. Each one was working ok, but the final one has the nicest structure of actions. How do you display the values that matter without writing complicated rules?
With simple filters. Create 4 display messages and corresponding IF statement. Compare it against the total value of seconds (X) and:
SS IF X < 60 (display seconds only) MM:SS IF X > 59 && X < 3600 (display min & sec only) HH:MM:SS IF X > 3599 && X < 86400 (display min & sec only) DD:HH:MM:SS IF X > 86399
More OCD in Seconds to DD:HH:MM:SS
I’m not done. The eagle-eyed readers noticed that my values under 10 have leading zero! so 9 seconds looks like this:
09s. This is something we have to take care of manually! Sadly there is no clever way of adding it, but I opted out for a Sub-task which will be performed for each variable %days, %hours, %minutes, %seconds
Perform Task – add leading 0
If you never used it, action Perform Task lets you specify up to 2 variables that will be sent to the subtask. I could take the advantage of that but for the sake of simplicity, I’m using one variable at the time %par1 and this is why the entire subtask use that variable (it’s also a hint to why the main task uses %par1 too! YES you guessed it this is a subtask I will use).
Time for lazy IF statements! It’s not inventive, you define ranges and then use Variable set to set the correct formatting of each range.
%par1 < 1
If we have no seconds we want
00. Yes, it’s cheating but it works!
%par1 < 10 && %par1 > 0
For anything bigger than 0 but needing leading 0 I use
%par1 > 9
Anything that doesn’t need a leading zero – well… you could skip it or set it to
%par1 just to please your OCD!
You can be also as smart as /u/EllaTheCat and:
The leading zero can be done without conditionals: add 100 then remove the 1 with Variable Section.
Lastly, let’s return the value – it means that this number will be now available in our main task.
Back to Seconds to DD:HH:MM:SS
It’s time to organise everything. After every day|hour|minute|second calculation I’m using perform task action to set the leading zeros and returning a custom variable. This is the variable that holds my final value. All I need now is to use the filters from above to display correctly formatted timer!
It’s been a while since I made a detailed tutorial that aims at beginners of Tasker! I hope you will find creative uses of this task. I will keep mine secret for now, but you should read about it soon!
If you want, you can totally go for the v2.0 where leading 0s are removed if the value is in a leading spot ie:
07m 15s vs
7m 15s. My OCD can live without these! But if you do! I will add your method to the end of the post with your permission. If you have any questions – hit this Reddit thread.
Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.