FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Script not working

 


devroom
Hi

I dont know much about script, but i tried to make a page where you can insert a date and then the script calculates the weeknumber.

The script is not working. Can somebody check the script out and find the bugs?


If you need more info, say it or pm it Cool


Thx and Grtz Very Happy

-----
Edited

Thx to muggle; the script is working now. Only on 1 / 2-1-2005 it gives weeknumber 53 Confused
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" language="JavaScript"><!--
function y2k(number) {
return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
  var when = new Date(year,month,day);
  var newYear = new Date(year,0,1);
  var modDay = newYear.getDay();
  if (modDay == 0) modDay=6; else modDay--;

  var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) -
                 Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;

  if (modDay < 4 ) {
    var weeknum = Math.floor((daynum+modDay-1)/7)+1;
  } else {
    var weeknum = Math.floor((daynum+modDay-1)/7);
    if (weeknum == 0) {
      year--;
      var prevNewYear = new Date(year,0,1);
      var prevmodDay = prevNewYear.getDay();
      if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
      if (prevmodDay < 4) weeknum = 53; else weeknum = 52;
    }
  }
  return + weeknum;
}


function PrintWeeknummer() {
document.bereken.nummer.value = getWeek(y2k(document.bereken.year.value),document.bereken.month.value,document.bereken.day.value);}
//--></script>
</head>

<body>
<form name="bereken" onSubmit="PrintWeeknummer(); return false;">
<table>
 <tr>
  <td>Dag</td><td>Maand</td><td>Jaar</td>
 </tr>
 <tr>
  <td><select type="select" name="day" class="inputbox" size="1">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
  <option value="11">11</option>
  <option value="12">12</option>
  <option value="13">13</option>
  <option value="14">14</option>
  <option value="15">15</option>
  <option value="16">16</option>
  <option value="17">17</option>
  <option value="18">18</option>
  <option value="19">19</option>
  <option value="20">20</option>
  <option value="21">21</option>
  <option value="22">22</option>
  <option value="23">23</option>
  <option value="24">24</option>
  <option value="25">25</option>
  <option value="26">26</option>
  <option value="27">27</option>
  <option value="28">28</option>
  <option value="29">29</option>
  <option value="30">30</option>
  <option value="31">31</option>
  </select></td>
  <td><select type="select" name="month" class="inputbox" size="1">
  <option value="0">1</option>
  <option value="1">2</option>
  <option value="2">3</option>
  <option value="3">4</option>
  <option value="4">5</option>
  <option value="5">6</option>
  <option value="6">7</option>
  <option value="7">8</option>
  <option value="8">9</option>
  <option value="9">10</option>
  <option value="10">11</option>
  <option value="11">12</option>
  </select></td>
  <td><select type="select" name="year" class="inputbox" size="1">
  <option value="2005">2005</option>
  <option value="2006">2006</option>
  </select></td>
 </tr>
</table>
<input type="submit" value="Bereken" /><br /><br />
Weeknummer = <input name="nummer" type="text" readonly="yes" size="1"></input>
</form>
</body>
</html>


Last edited by devroom on Tue Aug 29, 2006 9:52 am; edited 5 times in total
muggle
You can save the script locally on your computer and open it with IE. If you set IE to show you the errors you will revive the following syntax ones:
devroom wrote:
Code:

...
var now = new Date(year,month,day);

PrintWeeknummer() {
...


You didn’t define `PrintWeeknummer()` as function and you don’t need `now` variable at all. Even if you need it you can’t have the line above, because `year`, `month` and `day` variables aren’t defined in this context.
By correcting the code like this you’ll be able to execute it without syntax errors.
Quote:

var now = new Date(); //(year,month,day);

function PrintWeeknummer() {


Later in this line:
devroom wrote:
Code:

...
document.bereken.nummer.value = getWeek(y2k(now.getYear()),now.getMonth(),now.getDate());}
...

You’re not using the variables entered into the form, it shall be modified as follows:
Quote:

document.bereken.nummer.value = getWeek(document.bereken.year.value,document.bereken.month.value-1,document.bereken.day.value);}

Please note that the month value is corrected by -1.
After you perform all these changes your script will work.
You HAVE to test it, I can't say if it will work correctly for all possible input values.
That's what I'm seeing as issues in the script, but I’m not considering myself as a JavaScript expert, so maybe there is more to be said.
Stubru Freak
devroom wrote:
Only on 1 / 2-1-2005 it gives weeknumber 53 Confused


Seems right to me, a week is part of the year most of it's days are in (according to ISO).
So 1-1, 2-1 and 3-1 sometimes are in a week in the previous year, and 29-12, 30-12 and 31-12 sometimes are in a week the next year.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.