www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] Inserting input variables

  1. #1
    Join Date
    Feb 2011
    Posts
    7

    resolved [RESOLVED] Inserting input variables

    I don't know Javascript at all, but managed to google my way so far, but now Ií stuck on a simple question of format.

    I have two dropdown menus that provides me with Day (1-31) and Month (0-11), and I can get the selected values by writing "results.keys[0]" for Day-, or "results.keys[1]" for the Month-input. From this I am trying to get the Week-number, by using the code below.


    My question is only this: How do I replace the getDay() and getMonth() with my known values "results.keys[X]"? I don't know the language, and ain't sure if I should remove the "when." and "now." or not, etc. I've tried a ton of different ways, but can't get a result.

    Just a point in the right direction would make me very happy!
    Andrew.

    Code:
    <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;
    }
    
    var now = new Date();
    document.write('Week number = ' +
    getWeek(y2k(now.getYear()),now.getMonth(),now.getDate()));
    //--></script>

  2. #2
    Join Date
    Feb 2011
    Posts
    7
    I found another script that does the same, where it was easier to implement the known values to.

    Code:
     today = new Date();
                thisDay = Date.UTC(2011, results.month, results.days);
                thisDay = thisDay / (24 * 60 * 60 * 1000);
                thisDay = thisDay - Math.round((2011 - 1970) * 365.25);
                offset = 6;
                thisDay = thisDay + offset;
                thisWeek = Math.round(thisDay / 7) - 1;
    Although something is wrong in this, as it gets a few weeks wrong. But with some fine tuning, I'm sure it will do. Sorry about posting before I found this solution, I just needed a quick fix desperately.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles