How Can I Separate Hours and Minutes in a Date-Time Value?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! In determining system uptime I get back a value like 65.75 hours. How can I convert this value to “65 hours and 45 minutes”?

— JM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JM. We’ll see what we can do for you, but we have to be honest: this is a very sad day for the Scripting Guys. Thanks to the baseball playoffs the Scripting Guy who writes this column had never seen the beginning of Sunday Night Football on TV. This past Sunday he watched the opening of the show for the first time, and was shocked – shocked – by what he saw: One of the great rock-and-roll songs of all time – Joan Jett’s I Hate Myself for Loving You – being twisted (by Pink) into some futile attempt to replicate the success of the Monday Night Football theme song. Oh, Joan: say it isn’t so. (Editor’s Note: The Scripting Editor seems to be in the minority by liking the original and yet also kind of liking the Sunday Night version.)

Needless to say, that was very distressing. If Hank Williams, Jr. (who sings the Monday Night Football theme song) wants to sell out, well, so what? Good for him. But not Joan Jett. What’s next? Bob Dylan selling iPods? The Rolling Stones selling everything from life insurance to cars (or software)?

Never mind; we don’t want to know.

So does that mean that everything is for sale these days, that nothing is sacred anymore? Not quite. Fortunately, the best things in life – like scripts that can convert the value 65.75 into hours and minutes – are still free:

numValue = 65.75
arrValues = Split(numValue, ".")
numHours = arrValues(0)
numMinutes = (arrValues(1) / 100) * 60
Wscript.Echo numHours & " hours and " & numMinutes & " minutes"

As you can see, this script (brought to you without commercial interruption) starts out by assigning the time value – 65.75 – to a variable named numValue. That was easy. Now comes the hard part: how do we separate the integer portion of the value from the decimal portion of the value? That is, how do we separate the 65 and the 75?

What’s that? Oh, right: we’re supposed to answer that, aren’t we? Well, although there might be other ways to do this, we decided to use the VBScript Split function to split the value 65.75 on the decimal point (.). That’s what we do with this line of code:

arrValues = Split(numValue, ".")

When we execute that bit of code we get back an array (an array we named arrValues) consisting of two items:

65

75

By happy coincidence, those just happen to be the two values we wanted to extract from 65.75. Nice, huh?

OK; now that we have our two values what do we do with them?

Well that hardly seems fair; you mean we have to answer that question, too? Oh, very well. The 65 (which represents 65 hours) is fine just as it is; therefore we simply take that value and assign it to a variable named numHours. Because 65 is the first item in the array it has an index number of 0; therefore, we assign numHours the array item that has the index number of 0. In other words:

numHours = arrValues(0)

The 75 is a different story. After all, this doesn’t represent 75 minutes; it represents .75 hours, a value we need to convert to minutes. Fortunately (if not for you then for the Scripting Guy who writes this column) this doesn’t require much in the way of fancy mathematics; in fact, all we have to do is convert 75 to .75 (something we can do by dividing 75 by 100) and then multiply .75 by 60. Believe it or not, that will give us the number of minutes (.75 x 60 = 45). That’s what we do with this line of code:

numMinutes = (arrValues(1) / 100) * 60

Yes, it looks a little complicated, but it’s actually pretty straightforward. Because 75 is the second item in the array, it has the index number 1; that’s why we reference arrValues(1). As you can see, we simply take that value and divide it by 100, then take the result of that equation and multiply it by 60. And then we grab the product of those two numbers and stash it in a variable named numMinutes. At this point we’re all-but done; after all, we have the number of hours stored in the variable numHours and the number of minutes stored in the variable numMinutes. All we have to do now is echo back these two values:

Wscript.Echo numHours & " hours and " & numMinutes & " minutes"

When we do, we get back the following:

65 hours and 45 minutes

That should do the trick, JM. Thanks for the question, and don’t worry: unlike the rest of the world, the Scripting Guys have never sold out.

Note. OK, if you want to get technical, no one has ever actually asked the Scripting Guys if they would sell out. But, hey, let’s not quibble over minor details like that, OK? The important thing is that the Scripting Guys have never sold out, ever.

Although they are willing to listen to offers, if you know what we mean ….

0 comments

Discussion is closed.

Feedback usabilla icon