I need to convert hours into weeks, days, and hours.

My professor was hinting that there might be a way to
do this using the modulus operator but I can't seem to think of a way
that its possible. Can anyone help?

thanks!

Member Avatar for r.stiltskin

Well, if you use integer division to convert hours into days you would use mod to determine the remainder hours.

thanks, so would it looks something like this

//convert weeks, days, hours
	      days = hours / 24 %

i dont think thats right, I guess im not really sure how to use the mod in this situation.

Member Avatar for r.stiltskin

No, you must realize that what you wrote is not correct syntax. You can get the days part by

days = hours / 24;

Getting the remainder is a separate operation. Think about it.

Take for example you want to convert 7520 seconds into hh:mm:ss. Use integer math
To get hours: hr = sec/(60*60); // int math truncates What's left is: sec = sec - (hr*(60*60)); // remove the #hrs Or using mod: sec = sec % (60*60)); // gives remainder if hrs divided out From here you should be able to do minutes, then figure out how to do your project. It's the same.

Sorry, if im being difficult. I don't understand what is going on here

(hr*(60*60))

Thanks for your help so far, I do understand this more than I did 30 mins ago.
Edit: I understand whats going on as far as 60*60 = 3600 and thats how many seconds are in an hour. I still dont get how to use this for going from hours to days

I showed you how it works for hh:mm:ss from seconds.
Your task is to do weeks, days, hours from hours.
It's the same procedure with different numbers. How many hours in a day? How many hours in a week?

Simple as that.

//convert hours, into weeks, days, hrs
	days = hours / 24;
	hours = hours - (days*(12*2)); // remove the #hrs (to pull out whats left)
	hours = hours % ((12*2)); // gives remainder if hrs divided out (using MOD)

got it! thank you so much!

actually, looks like its even simpler than i thought.

days = hours / 24;	
	hours = hours - (days*(24)); // remove the #hrs (to pull out whats left)
	hours = hours % (24); // gives remainder of hrs divided out (using MOD)

Arrays Introducción

Hi Fabiana_1,

if you have a question then, please, start a new thread by clicking on Contribute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.