Forum Discussion
Adding and subtracting numbers as time on user click
Hi,
I have a course where a user needs to select some options and each have a time impact.
I'd like to display the time in an hours, minutes format.
I can easily count the time of each element so that its ends up at like 500 minutes, but who knows what 500 minutes is? (It's 8 hours btw)
I can't seem to figure out how to add the numbers in a time format so that it displays 1 hour rather than 60 minues, or 1 hour 30 minues rather than 90 minues etc etc.
Please help!
- DavidSchwartz-fCommunity Member
Here's a solution using Javascript. I found the code here: https://stackoverflow.com/questions/4687723/how-to-convert-minutes-to-hours-minutes-and-add-various-time-values-together-usi
Essentially, I've just taken the variable "minutes", and used Javascript to populate a text variable that will show hours and minutes for the given number of minutes.
Example: https://360.articulate.com/review/content/d6aa2c54-ffa5-4941-ab66-9fbec0cf55dc/review
I've attached the SL file here with the Javascript.
- juliWaldenGroupCommunity Member
can you give me a little more information? What formula are you using? What does the user input?
- LewisMasling-2aCommunity Member
A user selects a check box, each check box is assocciated with a text box that has description about a video that is XX in length.
Users do not need to select all the options, only those of interest. As they select they're options I'd like to display the accumulative amount of time that all selected videos will take to view (add deduct time if they unselect too) in hours and minutes.
So far I have a number variable called MM and am adding a set figure to that each time an option is selected, but it obviously counts past 60 and that's not what I want.
I'm not sure how to display it as 1 hour 0 minutes and so on rather than 0 hours 60 minutes etc.
Does that help?
- juliWaldenGroupCommunity Member
so you are totally minutes but what to display as hours:minutes
- LewisMasling-2aCommunity Member
yes
- SteveGannonCommunity Member
Lewis,
Use two variables, one for hours, the other for minutes. Using triggers, when minutes gets to 60, add 1 to hours and reset minutes to 0. Create a custom state for the text box displaying the time to accommodate the time when minutes is less than 10. In that custom state, insert a leading 0 (so that 4:9 displays as 4:09).
There are many, many timer examples in the forums to help get you started.
- juliWaldenGroupCommunity Member
I have to go into a meeting. but I think this is a solution here https://www.youtube.com/watch?v=NKL1ZAROBQE.
You need to add the checked boxes' time values in minutes to get a total and then divide by 60. This gives you the hours. Then subtract the multiple of 60 so if it is 2 hours you subtract 120 minutes from the total time. The remaining time is your minutes. I'll check back here later in the day to see if you need any more assistance.
A screenshot and a list of variables you are using would be helpful for us to communicate. You can send it privately to me if you wish to julie@waldengroup.net.Gotta run!
j
- LewisMasling-2aCommunity Member
David, thank you so much for sharing this with me.
I'm not a JavaScript expert but was able to follow the logic of your code and am pleased to say that with a little modification (changing NumericEntry to CheckBox selected) it works perfectly!!
Thanks so much! :)
- DavidSchwartz-fCommunity Member
Happy to help! I don't know Javascript really much at all, but like to find snippets when I can.