November 22nd, 2007

Hey, Scripting Guy! How Can I Tell If a Time Occurred in the Morning or the Evening?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Given a time of day, is there an easy way to tell whether that’s a morning time or an evening time?

— HG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HG. Can you feel the excitement in the year, that sense of anticipation, the crackling energy and the flow of adrenalin? That can only mean one thing: the annual Turducken Bowl is upon us.

On the off-chance that some of you have never heard of the annual Turducken Bowl, a few years ago the Scripting Guy who writes this column, the Scripting Son, and a pair of Scripting Nephews played a very spirited (and bruising) game of touch football on Thanksgiving Day. What started out as a way to pass the time until we could (finally!) sit down to eat has now become a holiday tradition: four downs to go the length of whatever field we’re playing on; 7 points per touchdown; first team to score 100 points wins. As is the case with most family activities, there is considerable controversy over which team holds the upper hand. As far as the Scripting Guy who writes this column is concerned, his team trails 4 wins to 3.

So how did last year’s game go? Well, remember, the important thing isn’t whether you win or lose; the important thing is just the sheer joy of taking part. Not that the Scripting Guy who writes this column actually experienced any kind of joy last year, let alone sheer joy. How did last year’s game go? Let’s put it this way: on the opening play of the game, the Scripting Guy who writes this column went out for a pass. His Scripting Nephew teammate, playing quarterback, flung the ball downfield, only to have the wind grab hold of the football and carry it over the fence and into the neighbor’s backyard.

Believe it or not, things actually got worse after that.

Unfortunately, things don’t promise to go much better this year. Due to some weird bureaucratic procedures that the Scripting Guy who writes this column doesn’t even pretend to understand, it was decided that this year the team rosters would be expanded by one player each. The Scripting Son and the Scripting Nephew (the nephew who didn’t throw a football over the fence), who won last year’s game in a rout, get to add one of the Scripting Brothers, a Scripting Brother who’s actually pretty good at football. In return, the Scripting Guy who writes this column and the Script Nephew (the one who did throw a football over the fence) get to add … one of the Scripting Significant Others. Is that a fair deal? Let’s put it this way: that’s about as fair as –

You know what? Maybe we shouldn’t put it that way after all. Instead, maybe we should just keep quiet and not get ourselves into any further trouble.

Besides, we can always hope that the Significant Other breaks her leg before the game starts.

Just kidding, dear. No one wants you to break your leg.

A twisted or sprained ankle would work just fine.

Anyway, the Scripting Guy who writes this column is busy trying to come up with a game plan that will help his team pull off the upset of the century. Has he come up with anything yet? No, not yet, or at least not anything that doesn’t involve kidnapping all the players on the other team. And even then he’s not convinced that his team could win.

One of the strategies the Scripting Guy who writes this column briefly considered was to write a script that could tell whether a specified time occurred in the evening or the afternoon. He discarded that strategy after he had trouble figuring out how that would help him win a football game. On the bright side, however, (and trust us, some of us are desperately searching for a bright side to all this) that strategy did help him to answer today’s question:

dtmTime = #11/21/2007 1:57 PM#

intHour = Hour(dtmTime)

If intHour > 11 Then Wscript.Echo “This is an afternoon/evening time.” Else Wscript.Echo “This is a morning time.” End If

You’re right: this is a very simple little script, isn’t it? (And it’s merely a coincidence that the Scripting Guy who writes this column picked a question that could be answered with a simple little script on the one day that he gets to leave work as soon as the column is done.) The script starts out by assigning a date-time value to a variable named dtmTime; in case, you’re wondering, the pound signs (#) that surround the value tell VBScript that this is a date-time value and not some other type of value:

dtmTime = #11/21/2007 1:57 PM#

So how do we know whether this time occurred in the morning (i.e., before 12:00 noon) or in the afternoon/evening (i.e., after 12:00 noon)? Well, we can do that easily enough; after all, we can see the PM designator stuck on the end. But how is the script supposed to know if this is a morning or evening time?

Here’s how:

intHour = Hour(dtmTime)

All we’re doing here is using the Hour function to grab just the hour portion of our date-time value. The Hour function returns an integer value between 0 and 23, representing the hour in 24-hour time; in our sample script, that means we’ll get back a 13 (because, in the 24-hour format, 1:57 PM is equivalent to 13:57). All we have to do then is determine whether the hour value we got back is greater than 11:

If intHour > 11 Then

If this is true (that is, if the hour is between 12 and 23, inclusive) then we echo back the fact that we have an afternoon/evening time. If this is false (that is, if the hour is between 0 and 11, inclusive) then we echo back the fact that this is a morning time. That’s all we have to do.

If only figuring out a way to win a football game was that easy, huh?

The nice thing about using this approach is that it will work regardless of the time format we’re using. For example, suppose we assigned a date-time value to the variable dtmTime using this line of code:

dtmTime = FormatDateTime(#11/21/2007 13:57#, vbShortTime)

Thanks to the FormatDateTime function, in this case dtmTime will be equal to 13:57; there won’t be a PM stuck at the end of the value. But that’s OK, but our script doesn’t rely on the AM/PM designator. Instead, it just grabs the hour (13) and goes from there.

Speaking of going somewhere, it’s time to hit the road and get ready for another Turducken Bowl. Like we said, this year’s game should be especially, uh, … interesting ….

Oh, well. Maybe the Scripting Guy who writes this column will get lucky and break his leg before the game starts. But that’s probably too much to hope for. See you all on Monday.

Note. In case you’re wondering, the Turducken Bowl is named after the turducken, a legendary Thanksgiving dish made by stuffing a chicken inside a duck, stuffing the duck inside a turkey, then somehow cooking and eating the thing. Actually, we just assumed that this was a legendary dish until last year, when we got email from a reader who’s actually eaten turducken. To the Scripting Guy who writes this column, having eaten a plate of turducken is on par with having eaten a plate of Bigfoot or Loch Ness Monster.

By the way, if you have eaten a plate of Bigfoot or Loch Ness Monster, drop us a line; we’d love to hear about thatexperience.

And for those of you outside the US, those of you still puzzling over the notion of stuffing a chicken inside a duck inside a turkey, the answer to your question is this: yes, only in America.

Author

0 comments

Discussion are closed.