A JavaScript TimeInterval object
A JavaScript TimeInterval object
Written by Ian Elliot   
Wednesday, 21 September 2011
Article Index
A JavaScript TimeInterval object
Methods and overriding
Extending Date

Trying it out and extending Date

Now you can try it out. For example:

var t= TimeInterval(1,0,0);
t.add(new TimeInterval(1,1,2,0));      

This creates a TimeInterval of 1 hour and then adds 1 day 1 hour and 2 seconds to it. The display shows:

1 Day(s) 2:2:0:0

Now we have one final problem.

You can do time interval arithmetic with the TimeInterval object but you often want to add a TimeInterval object to a Date object. You can already add a Date object to a TimeInterval object, but the problem is that the result is a TimeInterval object - which is usually not what you want. So the temptation is to give the existing Date object an add method of its own which accepts either a Date or a TimeInterval object. You can do this quite easily using the Prototype property but it is better not to. If you start extending built-in objects, then things can become confusing and any frameworks you are using might behave strangely.

A much better option is to add a new object based on the built-in object and then customize it by augmentation. In this case, for example, let's create a Date2 object, complete with an add method:

var Date2 = function(){
var obj = new Date();
obj.add = function(t){
if (t.getTime) {
return obj;

Now you can write things like:

var d = Date2();
d.add(TimeInterval(1, 0, 0));

to add 1 hour to the current date/time.

This looks good, but there is a problem. The Date object has a range of different ways it can be called with varying numbers of parameters. Our example only uses a default constructor for today's date/time. One solution is to use apply(this,arguments) but, while this works with other objects, it doesn't work with Date. The reason is that it is implemented in an idiosyncratic way. If you call Date without new in front of it then it always returns a formatted string for the date and never a Date object. This means you can use apply to create Date object.

The only solution in this case is to work through the possibilities, which is easy but tedious:

Date2 = function(){
var obj;
if(arguments.length==0) obj=new Date();
if(arguments.length==1) obj=new Date(
if(arguments.length==3) obj=new Date(
if(arguments.length==4) obj=new Date(
arguments[3]  );
if(arguments.length==5) obj=new Date(
arguments[3] ,arguments[4] );
if(arguments.length==6) obj=new Date(
arguments[3] ,arguments[4],arguments[5] );
if(arguments.length==7) obj=new Date(
arguments[3] ,arguments[4],arguments[5],
arguments[6] );

Now you can construct a Date2 object in exactly the same way as a Date object. (If you know a better way of doing this email me.)

From here you can add other methods and generally extend the Date2 object to work with the TimeInterval object.

Further reading

Speed dating - the art of the JavaScript Date object

Date Hacks - Doing JavaScript Date Calculations

Objects with values



Just JavaScript - The Function Object

The function object is the most important object in all of JavaScript and yet we tend to forget that it is an object. Understanding that JavaScript functions are objects is probably the most important [ ... ]

Just JavaScript - Functions Scope, Lifetime and Closure

When you first meet it closure seems like the craziest of ideas. However once you realize that JavaScript functions are objects and what this implies then it seems like a natural consequence.

Other Articles




Last Updated ( Tuesday, 10 September 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.