4 * Represents an interval of time
8 private var _totalMilliseconds : Number;
10 public function TimeSpan(milliseconds : Number)
12 _totalMilliseconds = Math.floor(milliseconds);
16 * Gets the number of whole days
18 * @example In a TimeSpan created from TimeSpan.fromHours(25),
19 * totalHours will be 1.04, but hours will be 1
20 * @return A number representing the number of whole days in the TimeSpan
22 public function get days() : int
24 return int(_totalMilliseconds / MILLISECONDS_IN_DAY);
28 * Gets the number of whole hours (excluding entire days)
30 * @example In a TimeSpan created from TimeSpan.fromMinutes(1500),
31 * totalHours will be 25, but hours will be 1
32 * @return A number representing the number of whole hours in the TimeSpan
34 public function get hours() : int
36 return int(_totalMilliseconds / MILLISECONDS_IN_HOUR) % 24;
40 * Gets the number of whole minutes (excluding entire hours)
42 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500),
43 * totalSeconds will be 65.5, but seconds will be 5
44 * @return A number representing the number of whole minutes in the TimeSpan
46 public function get minutes() : int
48 return int(_totalMilliseconds / MILLISECONDS_IN_MINUTE) % 60;
52 * Gets the number of whole seconds (excluding entire minutes)
54 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500),
55 * totalSeconds will be 65.5, but seconds will be 5
56 * @return A number representing the number of whole seconds in the TimeSpan
58 public function get seconds() : int
60 return int(_totalMilliseconds / MILLISECONDS_IN_SECOND) % 60;
64 * Gets the number of whole milliseconds (excluding entire seconds)
66 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123),
67 * totalMilliseconds will be 2001, but milliseconds will be 123
68 * @return A number representing the number of whole milliseconds in the TimeSpan
70 public function get milliseconds() : int
72 return int(_totalMilliseconds) % 1000;
76 * Gets the total number of days.
78 * @example In a TimeSpan created from TimeSpan.fromHours(25),
79 * totalHours will be 1.04, but hours will be 1
80 * @return A number representing the total number of days in the TimeSpan
82 public function get totalDays() : Number
84 return _totalMilliseconds / MILLISECONDS_IN_DAY;
88 * Gets the total number of hours.
90 * @example In a TimeSpan created from TimeSpan.fromMinutes(1500),
91 * totalHours will be 25, but hours will be 1
92 * @return A number representing the total number of hours in the TimeSpan
94 public function get totalHours() : Number
96 return _totalMilliseconds / MILLISECONDS_IN_HOUR;
100 * Gets the total number of minutes.
102 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500),
103 * totalSeconds will be 65.5, but seconds will be 5
104 * @return A number representing the total number of minutes in the TimeSpan
106 public function get totalMinutes() : Number
108 return _totalMilliseconds / MILLISECONDS_IN_MINUTE;
112 * Gets the total number of seconds.
114 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(65500),
115 * totalSeconds will be 65.5, but seconds will be 5
116 * @return A number representing the total number of seconds in the TimeSpan
118 public function get totalSeconds() : Number
120 return _totalMilliseconds / MILLISECONDS_IN_SECOND;
124 * Gets the total number of milliseconds.
126 * @example In a TimeSpan created from TimeSpan.fromMilliseconds(2123),
127 * totalMilliseconds will be 2001, but milliseconds will be 123
128 * @return A number representing the total number of milliseconds in the TimeSpan
130 public function get totalMilliseconds() : Number
132 return _totalMilliseconds;
136 * Adds the timespan represented by this instance to the date provided and returns a new date object.
137 * @param date The date to add the timespan to
138 * @return A new Date with the offseted time
140 public function add(date : Date) : Date
142 var ret : Date = new Date(date.time);
143 ret.milliseconds += totalMilliseconds;
149 * Creates a TimeSpan from the different between two dates
151 * Note that start can be after end, but it will result in negative values.
153 * @param start The start date of the timespan
154 * @param end The end date of the timespan
155 * @return A TimeSpan that represents the difference between the dates
158 public static function fromDates(start : Date, end : Date) : TimeSpan
160 return new TimeSpan(end.time - start.time);
164 * Creates a TimeSpan from the specified number of milliseconds
165 * @param milliseconds The number of milliseconds in the timespan
166 * @return A TimeSpan that represents the specified value
168 public static function fromMilliseconds(milliseconds : Number) : TimeSpan
170 return new TimeSpan(milliseconds);
174 * Creates a TimeSpan from the specified number of seconds
175 * @param seconds The number of seconds in the timespan
176 * @return A TimeSpan that represents the specified value
178 public static function fromSeconds(seconds : Number) : TimeSpan
180 return new TimeSpan(seconds * MILLISECONDS_IN_SECOND);
184 * Creates a TimeSpan from the specified number of minutes
185 * @param minutes The number of minutes in the timespan
186 * @return A TimeSpan that represents the specified value
188 public static function fromMinutes(minutes : Number) : TimeSpan
190 return new TimeSpan(minutes * MILLISECONDS_IN_MINUTE);
194 * Creates a TimeSpan from the specified number of hours
195 * @param hours The number of hours in the timespan
196 * @return A TimeSpan that represents the specified value
198 public static function fromHours(hours : Number) : TimeSpan
200 return new TimeSpan(hours * MILLISECONDS_IN_HOUR);
204 * Creates a TimeSpan from the specified number of days
205 * @param days The number of days in the timespan
206 * @return A TimeSpan that represents the specified value
208 public static function fromDays(days : Number) : TimeSpan
210 return new TimeSpan(days * MILLISECONDS_IN_DAY);
214 * The number of milliseconds in one day
216 public static const MILLISECONDS_IN_DAY : Number = 86400000;
219 * The number of milliseconds in one hour
221 public static const MILLISECONDS_IN_HOUR : Number = 3600000;
224 * The number of milliseconds in one minute
226 public static const MILLISECONDS_IN_MINUTE : Number = 60000;
229 * The number of milliseconds in one second
231 public static const MILLISECONDS_IN_SECOND : Number = 1000;