JavaScript Data Structures - A TimeInterval Object
Written by Ian Elliot   
Thursday, 16 April 2020
Article Index
JavaScript Data Structures - A 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));      
alert(t);

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) {
this.setTime(this.getTime()+t.getTime())
};
}
return obj;
}

Now you can write things like:

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

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 not use apply to create a 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(
arguments[0]);
if(arguments.length==3) obj=new Date(
arguments[0],arguments[1],arguments[2]);
if(arguments.length==4) obj=new Date(
arguments[0],arguments[1],arguments[2],
arguments[3]  );
if(arguments.length==5) obj=new Date(
arguments[0],arguments[1],arguments[2],
arguments[3] ,arguments[4] );
if(arguments.length==6) obj=new Date(
arguments[0],arguments[1],arguments[2],
arguments[3] ,arguments[4],arguments[5] );
if(arguments.length==7) obj=new Date(
arguments[0],arguments[1],arguments[2],
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

 


JavaScript Data Structures 

Cover

Contents

  1. The Associative Array
  2. The String Object
  3. The Array object
  4. Speed dating - the art of the JavaScript Date object
  5. Doing JavaScript Date Calculations
  6. A Time Interval Object
  7. Collection Object
  8. Stacks, Queue & Deque
  9. The Linked List
  10. A Lisp-like list
  11. The Binary Tree
  12. Bit manipulation
  13. Typed Arrays I
  14. Typed Arrays II
  15. Master JavaScript Regular Expressions
    * First Draft
 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962579>

<ASIN:1871962560>

<ASIN:0596517742>

<ASIN:0596806752>

 



Last Updated ( Saturday, 25 April 2020 )