Java Reference
In-Depth Information
However, that's not quite as simple as it may appear at first glance. For example, consider a departure
time of 14:53 (for 2:53 p.m.) and an arrival time of 15:10 (for 3:10 p.m.). If you tell JavaScript to evaluate
the expression 15.10-14.53, you get the result 0.57, which is 57 minutes. However, you know that the
real difference in time is 17 minutes. Using the normal mathematical operators on times doesn't work!
What would you need to do to calculate the difference between these two times? You would first
need to separate the hours from the minutes in each time. Then, to get the difference in minutes
between the two times, you would need to check whether the minutes of the arrival time were
greater than the minutes of the departure. If so, you could simply subtract the departure time
minutes from the arrival time minutes. If not, you'd need to add 60 to the arrival time minutes and
subtract one from the arrival time hours to compensate, before taking the departure time minutes
from the arrival time minutes. You'd then need to subtract the departure time hours from the arrival
time hours, before putting the minutes and hours that you have arrived at back together.
This would work okay so long as the two times were in the same day. It wouldn't work, for
example, with the times 23:45 and 04:32.
This way of working out the time difference obviously has its problems, but it also seems very
complex. Is there an easier way to deal with more complex data such as times and dates?
This is where objects come in. You can define your departure and arrival times as Date objects.
Because they are Date objects, they come with a variety of properties and methods that you can use
when you need to manipulate or calculate times. For example, you can use the getTime() method to
get the number of milliseconds between the time in the Date object and January 1, 1970, 00:00:00.
Once you have these millisecond values for the arrival and departure times, you can simply subtract
one from the other and store the result in another Date object. To retrieve the hours and minutes of
this time, you simply use the getHours() and getMinutes() methods of the Date object. You see
more examples of this later in the chapter.
The Date object is not the only type of object that JavaScript has to offer. Another object type was
introduced in Chapter 2, but to keep things simple, we didn't tell you what it was at the time: the
Array object. Recall that an array is a way of holding a number of pieces of data at the same time.
Array objects have a property called length that tells you how many pieces of data, or rather how
many elements, the array holds. They also have a number of methods. One example is the sort()
method, which you can use to sort the elements within the array into alphabetical order.
You should now have an idea why objects are useful in JavaScript. You have seen the Date and Array
objects, but JavaScript makes available many other types of objects so that you can achieve more with
your code. These include the Math and String objects, which we talk more about later in the chapter.
using javascript objects
Now that you have seen the why of JavaScript objects, you need to look at the what and the how .
Each of JavaScript's objects has a collection of related properties and methods that you can use to
manipulate a certain kind of data. For example, the Array object consists of methods to manipulate
arrays and properties to find out information from them. In most cases, to make use of these
methods and properties, you need to define your data as one of these objects. In other words, you
need to create an object.
 
Search WWH ::




Custom Search