About This Code
Brief Description:
function addDuration(time, duration)
Contributor:
Andrew Jones
Last Modified:
17 Jun 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
function addDuration(time, duration)
{
var thisform = document.forms[0];
var meridian = "";
var amstring = thisform.tmpMeridian.value.slice( 0, thisform.tmpMeridian.value.indexOf(";"));
var pmstring = thisform.tmpMeridian.value.slice( thisform.tmpMeridian.value.indexOf(";") + 1, thisform.tmpMeridian.value.length);
var returnstring = "";
var newstart = new Date( translateDate(thisform.StartDate.value) + " " + time);
if (newstart.toString() == "Invalid Date" || newstart.toString() == "NaN") return "NaN";
strTimeFormat = thisform.tmpDateTimeFormat.value.slice(thisform.tmpDateTimeFormat.value.indexOf(";") + 1, thisform.tmpDateTimeFormat.value.length)
strTimeFormat = stripWhitespace(strTimeFormat)
var num = parseInt(duration); // convert string to integer
var hours = newstart.getHours(); // 0 - 23
var minutes = newstart.getMinutes(); // 0 - 59
hours += num / 60;
if (hours > 23) // we have spanned days
{
var xxNoMidnight = 'Meetings can not span midnight';
alert( xxNoMidnight );
return "NaN";
}
if (strTimeFormat == "12")
{
if (hours > 12)
{
hours -= 12;
meridian = pmstring;
}
else if (hours < 12)
{
meridian = amstring;
} else {
meridian = pmstring;
}
}
if (hours < 9) hours = "0" + hours;
minutes += num % 60;
if (minutes < 9) minutes = "0" + minutes;
if (meridian.length > 0)
{
returnstring = hours + ":" + minutes + " " + meridian;
}
else
{
returnstring = hours + ":" + minutes;
}
return returnstring;
}
Usage / Example