
function TimerJS() {
	this.start();
}

//start the timer (wont start again even if called)
TimerJS.prototype.start = function() {
	if (!this.getStart()) {
		this.restart();
	}
}

//restart timer from zero (use instead of START)
TimerJS.prototype.restart = function() {
	this.startTime = new Date();
	this.endTime = null;
}

//returns true if timer has been started already
TimerJS.prototype.getStart = function() {
	return ((this.startTime != null) && (this.endTime == null));
}

//stop the timer
TimerJS.prototype.stop = function() {
	this.endTime = new Date();
}

//get elapsed time
TimerJS.prototype.getElapsed = function() {
	var end = new Date();
	if (this.endTime != null) {
		end = this.endTime;
	}
	
	var diffTime = new Date();
	diffTime.setTime(end.valueOf() - this.startTime.valueOf());
	
	return diffTime;
}




/*
$dt = new DateTime();
echo $dt->format("Y-m-d\TH:i:s.u") . "\n";
/**/
function time_format(time, value) {
	//' ['+diff.getSeconds()+'.'+str_pad(diff.getMilliseconds(), '0', 4, true)+' sec]';
	value = value.replace(/%y/g, time.getFullYear());
	value = value.replace(/%o/g, (time.getMonth()+1));
	value = value.replace(/%d/g, time.getDate());
	
	value = value.replace(/%h/g, time.getHours());
	value = value.replace(/%M/g, time.getMinutes());
	value = value.replace(/%m/g, str_pad(time.getMinutes(), 2, '0', true));
	value = value.replace(/%S/g, time.getSeconds());
	value = value.replace(/%s/g, str_pad(time.getSeconds(), 2, '0', true));
	value = value.replace(/%I/g, time.getMilliseconds());
	value = value.replace(/%i/g, str_pad(time.getMilliseconds(), 4, '0', true));
	return value;
}