Date() object and learn how to utilize it in our scripts.
All epoch time is measured in units of milliseconds. To ensure accuracy, all calculations should be performed on that basis. These calculations are done using the
getTime() method. For instance, using a specific time — Friday, September 22, 2006, 3:57:12 PM, we can calculate the time as 1,158,955,032,531 milliseconds from January 1, 1970. (Boy, time sure flies!) In order to determine how many years it's been since the epoch, we could use the following calculation:
Chance are you're pretty confused by now so let me try to make it simpler (with some help from Danny Goodman). The following calculations (presented as variables) should make it easier for you to see the relationship.
That should be easier to understand. You can also use that for your own calculations.
The Date Object
Date() constructor to obtain and manipulate the day and time in a script. The information is taken from the user's computer and is only as accurate as the user's system. It's important to remember that as it will determine the outcome of your scripts.
It's possible to manipulate the date using Coordinated Universal Time (UTC), which is basically the same as Greenwich Mean Time (GMT). (Actually, GMT is measured from noon whereas UTC is measured from midnight. However, few use the noon measurement and refer to GMT as if it were actually UTC.)1 That's a bit beyond our basic tutorial so we won't worry too much about that now. We'll look at that later, in a more advanced tutorial.
Note also that the
Date() object is case sensitive. Failure to observe that little fact will result in many headaches.
To use the
Date() we need to use the
new keyword. If you remember our discussion in Part 11, the
new keyword creates a new instance of the object. In this case it means that we take the
Date() object, make a copy of it and give that copy a name. It's a new "instance" or "occurrence" of the
Date() object. That would be done like this:
As I said, this creates a new instance of the
Date() object. It's important to remember that it contains the value of the user's system date at the time of execution. The date/time is not updated unless manipulated by the script. For instance, if I issued the above statement right now, it might contain the date/time data as listed below:
Fri Sep 22 2006 14:29:28 GMT-0400 (Eastern Standard Time)
While this will be fine in many situations, it's important to remember that, without manipulation, the data is not constantly being updated as the script is running. If we wanted to display the data as is, we could write the following:
which would print as follows:
The date is Friday, September 22, 2006
and the time is 2:29:28 PM
Let's take a look at what we did here.
Let's take a look at the methods available when using the
Date() object. From there we will look at how to apply them.
1 Taken from the following sources:
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...