Substring determines addition
I'm not a Javascript guru and would appreciate some help. Basically, I'm taking the difference between two dates and then displaying the numbers of weeks with a remainder. If the first remainder decimal is 7 or greater, they want the number rounded up to the next whole number. If it's a whole number, just tack on a ".0" for consistency. I've tried the following, but it doesn't seem to work. Can anybody help or is there a cleaner way to do it?
Code:
var x = document.getElementById('txtDueDate').value;
var y = document.getElementById('txtAdmit').value;
var dt1 = Date.parse(x)
var dt2 = Date.parse(y)
var ga = Math.round(((280 - (dt1.valueOf() - dt2.valueOf()) / (60 * 60 * 24 * 1000)) / 7) * Math.pow(10, 1)) / Math.pow(10, 1);
if (inStr(ga,".") = -1) {
document.getElementById('txtGaAdmit').value=ga + ".0"
}
else {
if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
document.getElementById('txtGaAdmit').value = (ga.subStr(0, inStr(ga, ".") - 1) + 1) + ".0"
}
else {
document.getElementById('txtGaAdmit').value=ga
}
}
Re: Substring determines addition
Do you know the line where the program has a trouble?
I guess there is a conversion to be done between strings of characters and numbers.
Try replacing:
Code:
if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
by:
Code:
if (Number(ga.subStr(inStr(ga,".")+1,1)) >= 7) {
Instead of Number(), you could use parseInt() or parseFloat().
Re: Substring determines addition
Are you just trying to get how many possible weeks and days total between two dates? If so, here is a simple function which will do just that. If not, can you explain yourself more clearly, please?
Code:
<script type="text/javascript">
function calculateWeeks(d1, d2) {
d1 = Date.parse(d1);
d2 = Date.parse(d2);
if (d1 > d2) {
alert("End date must come after start date.");
}
else {
var days = (d2 - d1) / 1000 / 60 / 60 / 24;
var weeks = Math.ceil(days / 7);
var leftover = days % 7;
var output = weeks + " weeks";
if (leftover > 0) {output += " and " + leftover + " days";}
alert(output);
}
}
calculateWeeks('5/28/2009', '6/23/2009');
</script>