自定义周(从7月开始的第1周),计算周数的代码并选择

时间:2013-12-03 01:30:27

标签: jquery datepicker

我有一个独特的情况......我有以下代码,目前允许我从一个组合框中选择一周引用的日期。我希望自动选择周(日期),具体取决于它的日期。例如,它是今天12月3日的第22周......如何默认组合框显示第22周?我应该用不同的方法做到这一点,因为将来周日期会发生变化吗?

<select name="dateentryweek" id="entryweek2" onclick="javascript:loadWeekOnly1()">
        <option value=""></option><option value="2013-07-07">Week 1</option><option value="2013-07-14">Week 2</option><option value="2013-07-21">Week 3</option><option value="2013-07-28">Week 4</option>
        <option value="2013-08-04">Week 5</option><option value="2013-08-11">Week 6</option><option value="2013-08-18">Week 7</option><option value="2013-08-25">Week 8</option>
        <option value="2013-09-01">Week 9</option><option value="2013-09-08">Week 10</option><option value="2013-09-15">Week 11</option><option value="2013-09-22">Week 12</option>
        <option value="2013-09-29">Week 13</option><option value="2013-10-06">Week 14</option><option value="2013-10-13">Week 15</option><option value="2013-10-20">Week 16</option>
        <option value="2013-10-27">Week 17</option><option value="2013-11-03">Week 18</option><option value="2013-11-10">Week 19</option><option value="2013-11-17">Week 20</option>
        <option value="2013-11-24">Week 21</option><option value="2013-12-01">Week 22</option><option value="2013-12-08">Week 23</option><option value="2013-12-15">Week 24</option>
        <option value="2013-12-22">Week 25</option><option value="2013-12-29">Week 26</option><option value="2014-01-05">Week 27</option><option value="2014-01-12">Week 28</option>
        <option value="2014-01-19">Week 29</option><option value="2014-01-26">Week 30</option><option value="2014-02-02">Week 31</option><option value="2014-02-09">Week 32</option>
        <option value="2014-02-16">Week 33</option><option value="2014-02-23">Week 34</option><option value="2014-03-02">Week 35</option><option value="2014-03-09">Week 36</option>
        <option value="2014-03-16">Week 37</option><option value="2014-03-23">Week 38</option><option value="2014-03-30">Week 39</option><option value="2014-04-06">Week 40</option>
        <option value="2014-04-13">Week 41</option><option value="2014-04-20">Week 42</option><option value="2014-04-27">Week 43</option><option value="2014-05-04">Week 44</option>
        <option value="2014-05-11">Week 45</option><option value="2014-05-18">Week 46</option><option value="2014-05-25">Week 47</option><option value="2014-06-01">Week 48</option>
        <option value="2014-06-08">Week 49</option><option value="2014-06-15">Week 50</option><option value="2014-06-22">Week 51</option><option value="2014-06-29">Week 52</option>

我的jquery datepicker也有这个代码,它在日期旁边正确显示周数,但不会根据当前周自动选择合适的日期......

function myWeekCalc(date) {
    var checkDate = new Date(date.getTime());
    // Find
    checkDate.setDate(checkDate.getDate() + 185 - (checkDate.getDay()));
    var time = checkDate.getTime();
    checkDate.setMonth(0); // Compare with Jan 1
    checkDate.setDate(1);
    return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
}

jQuery('input[name=selectweek1]').datepicker({
    dateFormat: 'yy-mm-dd',
    changeYear: true,
    changeMonth: true,
    firstDay: 1,
    calculateWeek: myWeekCalc,
    showWeek: true,
    minDate: new Date(2013, 6, 1),
    maxDate: new Date(2014, 5, 29)
});

JSFIDDLE:http://jsfiddle.net/a2nmR/

1 个答案:

答案 0 :(得分:0)

使用.filter().prop()

样本: http://jsbin.com/aviVeyUW/3/edit

  $('#entryweek2 option').filter(function(index) { 
      return index === myWeekCalc(new Date());   
  }).prop('selected', true);

您也可以这样做:

样本: http://jsbin.com/aviVeyUW/4/edit

$('#entryweek2 option').filter(function(index) { 
      var $this = $(this),
          today = new Date(),
          weekNo = parseInt($(this).text().split('Week ')[1]);
      return weekNo === myWeekCalc(today);
}).prop('selected', true);