wdatepicker 的可选时间范围设置

wdatepicker 的可选时间范围设置

可选时间为2014年以后 两个时间间隔在一年以内 这个是根据产品需求自己更改的插件 希望可以帮到有需要的朋友。
html页面的代码

    <input id="from" readonly="readonly" autocomplete="off" placeholder="开始日期"
                               class="item-li-content w37 fl bj-times"
                               onclick="WdatePicker({onpicked:function(){getListFormDate()},oncleared:function(){getListClearDate()},maxDate:getFromMaxDate(),minDate:getFromMinDate()})"
                               type="text">
                        <input id="to" readonly="readonly" autocomplete="off" placeholder="结束日期"
                               class="inp-ml30 item-li-content w37 fl bj-times"
                               onclick="WdatePicker({onpicked:function(){getListFormDate()},oncleared:function(){getListClearDate()},minDate:getToMinDate(),maxDate:getToMaxDate(),position:{left:-35,top:0}});"
                               type="text">
                    </p>


  //getListFormDate()  和getListClearDate()  是在input框里面输入内容或者没有内容的时候  选择完成以后触发的事情    

js页面的代码

  Date.prototype.Format = function (fmt) { //author: meizz
      var o = {
          "M+": this.getMonth() + 1, //月份
          "d+": this.getDate(), //日
          "h+": this.getHours(), //小时
          "m+": this.getMinutes(), //分
          "s+": this.getSeconds(), //秒
          "q+": Math.floor((this.getMonth() + 3) / 3), //季度
          "S": this.getMilliseconds() //毫秒
      };
      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o)
          if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
  };
  function getFromMinDate() {
      if (!$dp.$D('to')) {
          return '2014-01-01'
      }
      else {
          var year = ($dp.$D('to').M == 12 && $dp.$D('to').d == 31) ? $dp.$D('to').y :  $dp.$D('to', { y: -1 }).y;
          // var month = ($dp.$D('to').M == 12 && $dp.$D('to').d == 31) ? ($dp.$D('to').y.M < 10 ? '0' + $dp.$D('to').y.M : $dp.$D('to').y.M) : ($dp.$D('to', { y: -1 }).M < 10 ? '0' + $dp.$D('to', { y: -1 }).M : $dp.$D('to', { y: -1 }).M);

          // var year = $dp.$D('to', { y: -1 }).y;

          if($dp.$D('to').M == 12 && $dp.$D('to').d == 31){
              var month = $dp.$D('to', { y: 0,M: 1 }).M < 10 ? '0' + $dp.$D('to', { y: 0,M: 1 }).M : $dp.$D('to', { y: 0,M: 1 }).M;
          }else{
              var month = $dp.$D('to', { y: -1 }).M < 10 ? '0' + $dp.$D('to', { y: -1 }).M : $dp.$D('to', { y: -1 }).M;
          }

          var day = $dp.$D('to', { y: -1 }).d < 10 ? '0' + $dp.$D('to', { y: -1,d:1 }).d : $dp.$D('to', { y: -1 ,d:1}).d;
          if (year < 2014) {
              return '2014-01-01'
          }
          var date = year + '-' + month + '-' + day;
          return date;
      }
  }
  function getFromMaxDate() {
      if (!$dp.$D('to')) {
          return new Date().Format("yyyy-MM-dd");
      }
      else {
          var year = $dp.$D('to').y;
          var month = $dp.$D('to').M < 10 ? '0' + $dp.$D('to').M : $dp.$D('to').M;
          var day = $dp.$D('to').d < 10 ? '0' + $dp.$D('to').d : $dp.$D('to').d;
          var date = year + '-' + month + '-' + day;
          return date;
      }
  }
  function getToMinDate() {
      if (!$dp.$D('from')) {
          return '2014-01-01';
      }
      else {
          var year = $dp.$D('from').y;
          var month = $dp.$D('from').M < 10 ? '0' + $dp.$D('from').M : $dp.$D('from').M;
          var day = $dp.$D('from').d < 10 ? '0' + $dp.$D('from').d : $dp.$D('from').d;
          var date = year + '-' + month + '-' + day;
          return date;
      }

  }

  function getToMaxDate() {
      if (!$dp.$D('from')) {
          return new Date().Format("yyyy-MM-dd");
      } else {
          var year = ($dp.$D('from').M == 1 && $dp.$D('from').d == 1) ? $dp.$D('from', { y: 0 }).y :  $dp.$D('from', { y: 1 }).y;
          if($dp.$D('from').d == 1){
              var month = $dp.$D('from', { y: 1,M:-1 }).M < 10 ? '0' + $dp.$D('from', { y: 1,M:-1 }).M : $dp.$D('from', { y: 1,M:-1 }).M;
          }else{
              var month = $dp.$D('from', { y: 1 }).M < 10 ? '0' + $dp.$D('from', { y: 1 }).M : $dp.$D('from', { y: 1 }).M;
          }
          var day = $dp.$D('from', { y: 1 }).d < 10 ? '0' + $dp.$D('from', { y: 1,d:-1 }).d : $dp.$D('from', { y: 1,d:-1}).d;
          var nowYear = new Date().getFullYear();
          var nowMonth = new Date().getMonth() + 1;
          var nowDay = new Date().getDate();
          if (year > nowYear || (year == nowYear && month > nowMonth) || (year == nowYear && month == nowMonth && day > nowDay)) {
              return new Date().Format("yyyy-MM-dd");
          }
          var date = year + '-' + month + '-' + day;
          return date;
      }

  }

猜你喜欢

转载自blog.csdn.net/weixin_42872302/article/details/81432909