LA.NET [EN]

Mar 21

The AJAXToolkit introduces a Timespan class that lets you do some interesting things with time intervals. After looking at the class (which is defined on the datetime.js file, inside the common folder), it”s obvious that it tries to mimic the Timestamp class of the .NET platform. The good news is that you”ll be right at home if you have any experience with that class :,,)

You can create a new timespan in several ways. Lets start with the constructor. Internally, the class defines several auxiliary methods which are responsible for initializing the class” internal state (the number of ticks).  When you call the constructor with one parameter, you”re setting the number of ticks:

var timespan = new AjaxControlToolkit.TimeSpan( 260 ); //260 ticks

When you use 3 parameters, you”re defining the number of hours, minutes and settings stored in the timespan:

var timespance = new AjaxControlToolkit.TimeSpan(
                                                                       1, //hour
                                                                       20, //minutes
                                                                       30 //seconds
                                                                    );

Four parameter, means you”re also interested in setting the number of days:

var timespance = new AjaxControlToolkit.TimeSpan(
                                                                       3, //days
                                                                       1, //hour
                                                                       20, //minutes
                                                                       30 //seconds
                                                                    );

Finally, you can also specify the number of milliseconds by using a 5th parameter:

var timespance = new AjaxControlToolkit.TimeSpan(
                                                                       3, //days
                                                                       1, //hour
                                                                       20, //minutes
                                                                       30, //seconds
                                                                       10 //milliseconds
                                                                    );

And yes, you can also simply call the constructor without parameters. This will automatically set the internal tick count to zero. As you”ll see by the end of this post, the class has several “static” methods and the Date Object has also been enhanced with some new cool methods that help you in creating new timespans.

After getting a timespan, you have access to several methods that let you get more info on the current
interval:

  • getDays: returns the number of days in the current time interval. This is the largest unit you”ll get in a timespan object;
  • getHours: returns the number of hours in the timespan;
  • getMinutes: returns the number of minutes in the current interval;
  • getSeconds: surprise! returns the number of seconds in the timespan (yep, I bet you didn”t see that one coming! :) );
  • getMilliseconds: gives you the number of milliseconds in the timespan.

for instance, if you use the last constructor I”ve presented, and run the following code:

$get(“info”).innerHTML += “ticks: ” + timespan.getTicks() +”<br/>”;
$get(“info”).innerHTML += “days: ” + timespan.getDays() +”<br/>”;
$get(“info”).innerHTML += “hours: ” + timespan.getHours() +”<br/>”;
$get(“info”).innerHTML += “mins: ” + timespan.getMinutes() +”<br/>”;
$get(“info”).innerHTML += “secs: ” + timespan.getSeconds() +”<br/>”;
$get(“info”).innerHTML += “millisecs: ” + timespan.getMilliseconds() +”<br/>”;

where info is the ID of a div that exists on the page, you”ll get the following:

ticks: 2640300100000
days: 3
hours: 1
mins: 20
secs: 30
millisecs: 10

Simple, right? Well, there”s more! You can add and subtract two timespans:

var timespan = new AjaxControlToolkit.TimeSpan(
                                                                  3, //days
                                                                  1, //hour
                                                                  20, //minutes
                                                                  30, //seconds
                                                                  10 //miliseconds
);

var timespan2 = new AjaxControlToolkit.TimeSpan(
                                                                   1, //days
                                                                   1, //hour
                                                                   0, //minutes
                                                                   0, //seconds
                                                                   0 //miliseconds
  );

var subTimespan = timespan.subtract(timespan2)
$get(“info”).innerHTML += “ticks: ” + subTimespan.getTicks() +”<br/>”;
$get(“info”).innerHTML += “days: ” + subTimespan.getDays() +”<br/>”;
$get(“info”).innerHTML += “hours: ” + subTimespan.getHours() +”<br/>”;
$get(“info”).innerHTML += “mins: ” + subTimespan.getMinutes() +”<br/>”;
$get(“info”).innerHTML += “secs: ” + subTimespan.getSeconds() +”<br/>”;
$get(“info”).innerHTML += “ticks: ” + subTimespan.getMilliseconds() +”<br/>”;

Comparing is also possible: you can see if two timespanes are equal by calling (yes, you”ve guessed :) ) the equals method. There”s also a compareTo method which returns an integer (positive means the current timespan is bigger than the one passed as an argument to the method). Finally, you can also use a format method (which you can use to get a custom string that represents the timespan)  and a parse method (which tries to build a timespan object from string). The following method shows how you”re supposed to use these methods (btw, pay attention to the string I”ve used in the format method: i”ve used that string because that”s the format the parse method understands when you want to parse a “complete” timespan – ie, a timespan that has days, hours, minutes, seconds and milliseconds):

var timespan = new AjaxControlToolkit.TimeSpan(
                                                                  3, //days
                                                                  1, //hour
                                                               &
nbsp;  20, //minutes
                                                                  30, //seconds
                                                                  10 //miliseconds
);

var str = timespan.format(“dd:hh:mm:ss.nnnn”);
alert( str );

var timespan2 = AjaxControlToolkit.TimeSpan.parse( str );
alert( timespan2.getHours() );

Even though you can use the constructor to create a new instance of a timespan, the truth is that you”ll end up using one of the “static” auxiliary methods introduced by the class: fromTicks, fromHours, fromMinutes, fromSeconds and fromMilliseconds. These methods behave exactly like you”d expect (ex.: fromTicks creates a new instance of a timespan from the number of ticks you send to the method).

As I”ve said in the beginning of this post, the Date object has been enhanced with several new methods that will give you a timespan. You can get the current time of the day as timespan (getTimeOfDay), get the date (only – ie, stripped off of time) as timespan (getDateOnly), add or subtract a timespan (add/subtract methods) and count the number of ticks in a date (getTicks). Note that these methods have been added to the prototype  property of the Date Object and you can call them on instances of that type!

And that”s all for today! I”ll come back tomorrow with more info on the toolkit classe!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>