# Thread: calculate # of weeks between two dates

1. ## calculate # of weeks between two dates

I'm an extreme noob to javascript so any help would be very appreciated.

I have two date fields that are entered into a form and then have another text box where the user enters the number of weeks between these two dates. For example the user enters:
date1: 1/1/2006
date2: 1/12/2006
Then the user would enter the number of weeks in the next text box.
weeks: 1.71
Can someone tell me how I can automatically calculate the number of weeks using javascript so the user doesn't have to enter "1.71"?

2. Here's a sample script I wrote that uses a function similar to VB's DateDiff, it calculated the difference between two dates in the interval specified.

Code:
```<html>
<title>Date Diff</title>
<script type="text/javascript">
var intervals = {
s : 1000,
m : 60000,
h : 3600000,
d : 86400000,
w : 604800000
}

function dateDiff(interval,date1,date2) {
if( "smhdw".indexOf(interval) == -1 ) {
return "error";
}
var d1 = Date.parse(date1);
if( !d1 )
return "error";

var d2 = Date.parse(date2);
if( !d2 )
return "error";

var diff = (d2 - d1) / intervals[interval];
return diff;
}

</script>
<body>
Date 1 <input type="text" id="date1" value="" /><br>
Date 2 <input type="text" id="date2" value="" /><br>
Interval <select id="interval">
<option value="s">seconds</option>
<option value="m">minutes</option>
<option value="h">hours</option>
<option value="d">days</option>
<option value="w">weeks</option>
</select>
<input type="button" onclick="alert('Difference is ' + dateDiff(document.getElementById('interval').value,document.getElementById('date1').value,document.getElementById('date2').value));" value="Calculate Difference" />
</body>
</html>```

3. jimfraser,
i tried it out but it doesn't do anything; there is a page error that says that an object is required on line 41.
Maybe I'm putting the date in the wrong format.
I've tried several but haven't had any luck. Can you tell me what date format it's suppose to be in?

4. never mind i got it to work. there were just a couple spaces in the wrong place when i copied it over. thanks jimfraser.

5. can someone tell me how to return the result with only 2 decimal places?

6. x = Math.round(100 * number) / 100

7. jimfraser, thanks for all your help on this one. I appreciate it.

I've been doing some testing. Here's some of the results i'm getting:

date1:04/01/2006
date2:04/15/2006
weeks:1.99
(I thought it would be over 2 weeks, since it's 15 days)

date1:04/01/2006
date2:04/15/2006
weeks:1.85
(I thought it would be exactly 2 weeks, since it's 14 days)

date1:01/01/2006
date2:12/31/2006
weeks:52
(This looks correct)

Is there something I'm missing or doing wrong?

8. You don't count the day you are on, that's like day 0.

Today is the 5th - how many days till the 10th? 5, not 6.

If you want, add 1 to the difference

9. If it doesn't count the day you're one then my example that shows April 1st to 15th would have been exactly 2 weeks. That's exactly 14 days. But it only shows 1.99 weeks. I'm wondering why it doesn't calculate exactly 2 weeks.

date1:04/01/2006
date2:04/15/2006
weeks:1.99

10. That might be because I messed up the rounding off function - with no rounding on my pc it shows 2 exactly - checking

11. This worked for me - try it out.

First declare this prototype:
Code:
```Number.prototype.round = function(dp) {
var y = Math.pow(10,dp);
return Math.round(this*y)/y;
}```
then round your number like this:

myNumber.round(2);

12. I'm getting some weird results. Depending on the month I get a different value:

01/01/2006
01/15/2006
equals 2 weeks

04/01/2006
04/15/2006
equals 1.99 weeks

i retested it and it only happends in the month of April. every other month that I tested the first two weeks of all equalled two. Is there a reason April calculates slightly differently?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•