calculating number of minutes, hours and days by m

# Thread: calculating number of minutes, hours and days by m

1. Senior Member
Join Date
Dec 1969
Posts
171

## calculating number of minutes, hours and days by m

I had to calculate server downtime for previous week. iDowntime is stored in database as int. I used the code below and it works fine. Now, I need to do the same for months. This is more difficult as the number of minutes, hours and days varies by month and I am stumped. The only thing I can think of is to write a case statement for each month. Is there a better way to do this?<BR><BR>if iDownTime &#060;=60 then<BR>&#039;calculate percent downtime<BR>&#039;divide by 1 to get minutes <BR>&#039;divide result by num of minutes in week <BR>&#039;multiply by 100 to get percent<BR> Duration = iDowntime<BR> dLength = "m"<BR> iDownTime = iDownTime/1/10080*100<BR>elseif iDownTime &#060; 1440 then<BR>&#039;calculate percent downtime<BR>&#039;divide by 60 to get hours <BR>&#039;divide result by num of hours in week <BR>&#039;multiply by 100 to get percent<BR> Duration = iDowntime/60<BR> dLength = "h"<BR> iDownTime = iDownTime/60/168*100<BR>elseif iDownTime &#062;=1440 then<BR>&#039;calculate percent downtime<BR>&#039;divide by 1440 to get days <BR>&#039;divide result by num of days in week <BR>&#039;multiply by 100 to get percent<BR> Duration = iDowntime/1440<BR> dLength = "d"<BR> iDownTime = iDownTime/1440/7*100<BR>end if

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## HUH?

Why does it matter how long the month is???<BR><BR>All you want is the total downtime in that month, no?<BR><BR>OH! You want the PERCENTAGE???<BR><BR>Man, did you go about it the hard way!<BR><BR>What I don&#039;t get *AT ALL* is why it matters whether iDownTime is &#060;= 60, &#060; 1440, &#062;= 1440! A percentage is a percentage is a percentage.<BR><BR>All you want to do is divide the iDownTime by the number of minutes in whatever period you are working with. Period:<BR><BR>&#060;%<BR>Percentage = 100.0 * ( iDownTime / 60*24*7 )<BR>%&#062;<BR><BR>Presto. You are done. <BR><BR>Oh, I see. You just screwed up. You really do convert iDownTime to a percentage using the exact formula, no matter what. (Though you use weird variations on the formula, don&#039;t you?)<BR><BR>It&#039;s the *DURATION* that you want. But then it seems kind of screwy. Why would you want a duration of<BR> 1.1063888889 d<BR>Instead of<BR> 1 day 1 hour 32 minutes<BR>????<BR><BR>***********<BR><BR>Anyway, getting total # of minutes in a given month is easy:<BR><BR>&#060;%<BR>yr = 2003 &#039; or year you are interested in<BR>mn = 8 &#039; August, or month you are interested in<BR>startOfMonth = DateSerial( yr, mn, 1 )<BR>startOfNextMonth = DateSerial( yr, mn+1, 1 )<BR>numberOfDaysInMonth = startOfNextMonth - startOfMonth<BR>numberOfMinutesInMonth = 60*24*numberOfDaysInMonth<BR>%&#062;<BR><BR>Wouldn &#039;t you rather have<BR> 1 day 1 hour 32 minutes<BR>instead of the funky stuff you are getting now?<BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
171

## oh boy

I need both duration and percentage. I tend to take the long route in my coding :( Yes, I would prefer not to have the funky stuff, I had to end up rounding it.<BR><BR>Of course, now I am confused about what to use. I need to show duration and percentage and I would like to format it with days, hours and minutes.

4. Senior Member
Join Date
Dec 1969
Posts
171

## let's start over

Ok, I need to calculate the percentage of time a server is down each month. This job will run automatically on the 1st of every month for the previous month. I also need to calculate the duration of time in days/hours/min. <BR><BR>What is the best way to go about doing this so I don&#039;t have to change the code every month or manually run it every month?

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## So easy enough...

Oh...and I remembered an easier way to get number of days in a month! DOH on me!<BR><BR><BR>&#060;%<BR><BR>Sub GetDurationAndPercentage( BYVAL downTime, BYVAL numberOfDays, BYREF Percentage, BYREF Duration )<BR> dim d,h,m<BR> &#039; percentage is dead easy:<BR> Percentage = 100.0 * ( iDownTime / (60 * 24 * numberOfDays) )<BR> &#039; get duration in pretty form:<BR> d = downTime ( 24*60 ) <BR> downTime = downTime MOD (24*60)<BR> h = downTime 60<BR> m = downTime MOD 60<BR> Duration = d & " days, " & h & " hours, " & m & " minutes"<BR>End Sub<BR><BR>&#039; get info for a given month:<BR><BR>&#039; first calculate days in that month:<BR>yr = 2003 &#039; or year you are interested in <BR>mn = 8 &#039; August, or month you are interested in <BR>&#039; Isn&#039;t this next line incredibly simple!<BR>numberOfDaysInMonth = Day( DateSerial( yr, mn+1, 0 )<BR><BR>Dim DurationForMonth, PercentageForMonth<BR><BR>GetDurationAndPercentage DownTimeForMonth, numberOfDaysInMonth, PercentageForMonth, DurationForMonth<BR><BR>Response.Write "Downtime for " & MonthName(mn) & ", " & yr _<BR> & " was " & DurationForMonth & "&#060;br/&#062;"<BR>Response.Write "Percentage of downtime for " & MonthName(mn) & ", " & yr _<BR> & " was " & FormatNumber(PercentageForMonth,2) & "%"<BR><BR><BR>&#039; get info for given week:<BR>Dim DurationForWeek, PercentageForWeek<BR><BR>GetDurationAndPercentage DownTimeForWeek, 7, PercentageForWeek, DurationForWeek<BR><BR>Response.Write "Downtime for that week was " & DurationForMonth & "&#060;br/&#062;"<BR>Response.Write "Percentage of downtime for that week was " & FormatNumber(PercentageForWeek,2) & "%"<BR><BR>%&#062;<BR><BR>**************<BR><BR>Us ual warranty from me: 3 seconds or until I view next posting, whichever comes first. 100% off the top of my head and untested. Have fun.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Two UNRELATED questions

What is the best way to go about doing this so I don&#039;t have to change the code every month?<BR><BR>See code in other message. It&#039;s dead easy:<BR> daysInPriorMonth = Day( DateSerial( Year(Date), Month(Date), 0 ) )<BR>and from there hopefully it&#039;s obvious.<BR><BR>What is the best way to go about doing this so I don&#039;t have to manually run it every month?<BR><BR>By using WSH (Windows Script Host) and a ".vbs" (*NOT* an ASP page!) file and scheduling that through the Control Panel&#039;s Task Scheduler. To read up on WSH, go to<BR> http://msdn.microsoft.com/scripting<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
171

## Ok, I will try and test - thanks EOP

:)

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oh, what the heck...

A couple of minor typos in the code, but it worked.<BR><BR>Here&#039;s a working page:<BR><BR>*****************<BR>&#060;HTML&#062; &#060;BODY&#062;<BR><BR>&#060;% <BR><BR>Sub GetDurationAndPercentage( BYVAL downTime, BYVAL numberOfDays, BYREF Percentage, BYREF Duration ) <BR> dim d,h,m <BR> &#039; percentage is dead easy: <BR> Percentage = 100.0 * ( DownTime / (60 * 24 * numberOfDays) ) <BR> &#039; get duration in pretty form: <BR> d = downTime ( 24*60 ) <BR> downTime = downTime MOD (24*60) <BR> h = downTime 60 <BR> m = downTime MOD 60 <BR> Duration = d & " days, " & h & " hours, " & m & " minutes" <BR>End Sub <BR><BR>&#039; get info for a given month: <BR><BR>&#039; first calculate days in that month: <BR>yr = 2003 &#039; or year you are interested in <BR>mn = 8 &#039; August, or month you are interested in <BR>&#039; Isn&#039;t this next line incredibly simple! <BR>numberOfDaysInMonth = Day( DateSerial( yr, mn+1, 0 ) )<BR><BR>&#039; DUMMY TEST: <BR>DownTimeForMonth = 4577<BR><BR>Dim DurationForMonth, PercentageForMonth <BR><BR>GetDurationAndPercentage DownTimeForMonth, numberOfDaysInMonth, PercentageForMonth, DurationForMonth <BR><BR>Response.Write "Downtime for " & MonthName(mn) & ", " & yr _ <BR> & " was " & DurationForMonth & "&#060;br/&#062;" <BR>Response.Write "Percentage of downtime for " & MonthName(mn) & ", " & yr _ <BR> & " was " & FormatNumber(PercentageForMonth,2) & "%&#060;HR&#062;" <BR><BR><BR>&#039; get info for given week: <BR>Dim DurationForWeek, PercentageForWeek <BR><BR>&#039; DUMMY TEST: <BR>DownTimeForWeek = 1532<BR><BR>GetDurationAndPercentage DownTimeForWeek, 7, PercentageForWeek, DurationForWeek <BR><BR>Response.Write "Downtime for that week was " & DurationForWeek & "&#060;br/&#062;" <BR>Response.Write "Percentage of downtime for that week was " & FormatNumber(PercentageForWeek,2) & "%" <BR><BR>%&#062; <BR><BR>&#060;/BODY&#062;&#060;HTML&#062;<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
171

## You are the best!

I really appreciate it!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•