Skip to content

Instantly share code, notes, and snippets.

@troygrosfield
Created February 27, 2014 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troygrosfield/9250959 to your computer and use it in GitHub Desktop.
Save troygrosfield/9250959 to your computer and use it in GitHub Desktop.
This handles parsing of a duration string in javascript which allows for simple arithmetic.
/* Duration is a class that allows adding to or subtracting from dates.
* It's backed by a javascript date object, so this only works for milliseconds,
* not, microseconds.
*
* Note: This only deals with hours, minutes and seconds up to 23:59:59.999
*
* * param duration: the string duration value. (i.e. "01:25:35.123")
*
* Examples:
*
* >>> new Duration('12:35:45.123').getTimeString()
* "12:35:45.123"
* >>> new Duration('12:35:45.123').addSeconds(5).getTimeString()
* "12:35:50.123"
* >>> new Duration('12:35:45.123').addMinutes(2).addSeconds(5).getTimeString()
* "12:37:50.123"
* >>> new Duration('12:35:45.123').addMinutes(2).addSeconds(5).addMilliseconds(100).getTimeString()
* "12:37:50.223"
* >>> new Duration('5:45.123').addMinutes(2).addSeconds(5).addMilliseconds(100).getTimeString()
* "0:07:50.223"
* >>> new Duration('5:45.123').addMinutes(2).addSeconds(5).getTimeString()
* "0:07:50.123"
*/
function Duration(duration){
var hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0;
if (duration != undefined) {
parsed = duration.split(':');
parsed.reverse();
hours = (parsed.length > 2) ? parseInt(parsed[2]) : 0;
minutes = (parsed.length > 1) ? parseInt(parsed[1]) : 0;
seconds = parseInt(parsed[0]);
milliseconds = this.parseMilliseconds(parsed[0]);
}
this.date = new Date(0, 0, 0, hours, minutes, seconds, milliseconds);
}
// Get the milliseconds from a time string.
Duration.prototype.parseMilliseconds = function(seconds) {
if (seconds.indexOf('.') === -1) {
// no decimal found, no milliseconds
return 0;
}
milli_string = seconds.split('.')[1];
if (milli_string.length > 3) {
milli_string = milli_string.slice(0, 3);
} else if (milli_string.length < 3) {
while (milli_string.length < 3) {
milli_string = milli_string + '0';
}
}
return parseInt(milli_string);
}
/* Get formatted string time.
*
* 1:25:35.123
*/
Duration.prototype.getTimeString = function() {
var hours = this.getHours(),
minutes = this.getMinutes(),
seconds = this.getSeconds(),
milliseconds = this.getMilliseconds(),
output_time = '';
output_time += hours + ':' +
this.leftPad(minutes, 2) + ':' +
this.leftPad(seconds, 2);
if (milliseconds > 0) {
output_time += '.' + this.leftPad(milliseconds, 3);
}
return output_time;
}
Duration.prototype.leftPad = function(pad_this, num_times, pad_with) {
var output = pad_this.toString(),
pad_with = (!pad_with) ? '0' : pad_with;
while (output.length < num_times) {
output = pad_with + output;
}
return output;
}
Duration.prototype.getHours = function() {
return this.date.getHours();
}
Duration.prototype.setHours = function(hours) {
this.date.setHours(hours);
}
Duration.prototype.addHours = function(hours) {
this.date.setHours(this.date.getHours() + hours);
return this;
}
Duration.prototype.getMinutes = function() {
return this.date.getMinutes();
}
Duration.prototype.setMinutes = function(minutes) {
this.date.setMinutes(minutes);
}
Duration.prototype.addMinutes = function(minutes) {
this.date.setMinutes(this.date.getMinutes() + minutes);
return this;
}
Duration.prototype.getSeconds = function() {
return this.date.getSeconds();
}
Duration.prototype.setSeconds = function(seconds) {
this.date.setSeconds(seconds);
}
Duration.prototype.addSeconds = function(seconds) {
this.date.setSeconds(this.date.getSeconds() + seconds);
return this;
}
Duration.prototype.getMilliseconds = function() {
return this.date.getMilliseconds();
}
Duration.prototype.setMilliseconds = function(milliseconds) {
this.date.setMilliseconds(milliseconds);
}
Duration.prototype.addMilliseconds = function(milliseconds) {
this.date.setMilliseconds(this.date.getMilliseconds() + milliseconds);
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment