January 25, 2021
Hot Topics:

Dates and Times

  • By Joe Burns
  • Send Email »
  • More Articles »

When I first started putting up JavaScripts, and now this PERL series, one of the big things people want is the ability to post the date and the time. No sweat. This primer is dedicated to just that. I'll tell you that it's little rogher than you might think, but stay with me. We'll get the dates and times up and running.

Display the date and time

See the time: See the Script

(This will open in a new window): Here's the code

The Funny Thing About Dates and Times

The funny thing about dates and times, and one of the hardest things to get across to students is that computer don't see dates and times like you do. Computers see only numbers. You have to force a computer to display text. Furthermore, computers always start their number counts at zero. If the days of the week start with Sunday, then a computer sees Sunday as day zero. That sort off puts computer returns off by one.

Further furthermore, this doesn't hold true across elements. For instance, it's true for days of the week, months, and the hour of the day, but not for day of the month, minute or second. Confused?

Take heart. I put together the format I'm about to give you a good six months ago to be able to collect and time stamp some data. i wanted to put together a module I could simply copy and paste into a PERL script and gets and date or time element I wanted...and for that element to be correct.

The script you should now have open in that second window is what I came up with. let's look at it.

The Script

Every element of time is contained within a the command (function really) "localtime". What you basically do is set up some code to pluck out just what you want from localtime. In my code I grab these elements:

  • $sec (seconds)
  • $min (minutes)
  • $hour (the hour, 00-24)
  • $mday (day of the month)
  • $mon (month, 0-11)
  • $year (year, 2000 is "100")
  • $wday (day of the week, 1 - 7)
  • $yday (day of the year, 1-365 (6))

I have assigned each of those elements to localtime(time).

Next I have three arrays, one for the hour, the month, and the day. This is how I force numbers to become text.

The format is simple and cannot be changed:

  • A scalar variable (with the $ sign) is assigned the value of the array.
  • The array elements are within (parentheses) separated by commas, no spaces.
  • Next, square brackets denote where the number return for the array will come from (localtime)
  • Finally, the number in the second set of brackets denotes the item in the first array ($sec,$min,$hour, etc.)

Let me actually explain further by using the third array first. That's the one that represents "$thisday".

I have an array of the days of the week starting with Sunday. I know a number, zero through six, will be returned depending on the day of the week. I also know zero is Sunday. That's why I started with Sunday.

The return will come from (localtime) and I want the localtime array number 6. If you count the items in the localtime array left to right, starting at zero, you get $wday, the day of the week.

The day I wrote this tutorial was Thursday. The return from "$wday" was four. Count over zero, one, teo, three, four in the days array, you get Thursday.

Thursday is returned.

It's the same story with the hours and the months. The reason I did the hours was that the hours come back in military time. By putting in an array 1 to 12, 1 to 12, I get turn the 24 hour clock into a 12 hour return. get it.

OK, that's the thing I came up with (just those four lines) that I simply copy and paste into any PERL script if I want any date or time element. With those things in place, just under the PERL path, I can call on what I want.

By the way, the reason I only have those four arrays is that those are the only four elements that need them. Everything else returns correct numbers...except that $year, but we'll get to that in a minute.

Page 1 of 2

This article was originally published on May 8, 2000

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date