
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 <=60 then<BR>'calculate percent downtime<BR>'divide by 1 to get minutes <BR>'divide result by num of minutes in week <BR>'multiply by 100 to get percent<BR> Duration = iDowntime<BR> dLength = "m"<BR> iDownTime = iDownTime/1/10080*100<BR>elseif iDownTime < 1440 then<BR>'calculate percent downtime<BR>'divide by 60 to get hours <BR>'divide result by num of hours in week <BR>'multiply by 100 to get percent<BR> Duration = iDowntime/60<BR> dLength = "h"<BR> iDownTime = iDownTime/60/168*100<BR>elseif iDownTime >=1440 then<BR>'calculate percent downtime<BR>'divide by 1440 to get days <BR>'divide result by num of days in week <BR>'multiply by 100 to get percent<BR> Duration = iDowntime/1440<BR> dLength = "d"<BR> iDownTime = iDownTime/1440/7*100<BR>end if

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't get *AT ALL* is why it matters whether iDownTime is <= 60, < 1440, >= 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><%<BR>Percentage = 100.0 * ( iDownTime / 60*24*7 )<BR>%><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't you?)<BR><BR>It'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><%<BR>yr = 2003 ' or year you are interested in<BR>mn = 8 ' 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>%><BR><BR>Wouldn '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>

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.

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't have to change the code every month or manually run it every month?

So easy enough...
Oh...and I remembered an easier way to get number of days in a month! DOH on me!<BR><BR><BR><%<BR><BR>Sub GetDurationAndPercentage( BYVAL downTime, BYVAL numberOfDays, BYREF Percentage, BYREF Duration )<BR> dim d,h,m<BR> ' percentage is dead easy:<BR> Percentage = 100.0 * ( iDownTime / (60 * 24 * numberOfDays) )<BR> ' 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>' get info for a given month:<BR><BR>' first calculate days in that month:<BR>yr = 2003 ' or year you are interested in <BR>mn = 8 ' August, or month you are interested in <BR>' Isn'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 & "<br/>"<BR>Response.Write "Percentage of downtime for " & MonthName(mn) & ", " & yr _<BR> & " was " & FormatNumber(PercentageForMonth,2) & "%"<BR><BR><BR>' 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 & "<br/>"<BR>Response.Write "Percentage of downtime for that week was " & FormatNumber(PercentageForWeek,2) & "%"<BR><BR>%><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>

Two UNRELATED questions
What is the best way to go about doing this so I don't have to change the code every month?<BR><BR>See code in other message. It's dead easy:<BR> daysInPriorMonth = Day( DateSerial( Year(Date), Month(Date), 0 ) )<BR>and from there hopefully it's obvious.<BR><BR>What is the best way to go about doing this so I don'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's Task Scheduler. To read up on WSH, go to<BR> http://msdn.microsoft.com/scripting<BR><BR>

Ok, I will try and test  thanks EOP

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

You are the best!
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

