时期日期相关JS

<script language= " javascript ">


           var empNo, ajaxValue;

          $(function () {

              $( " input[title='员工编号'] ").bind( ' propertychange ', function () {
                  empNo =  this.value;
                  setUserDay(empNo,  "");
              })

              cuid = document.getElementById( " ctl00_m_g_2c17528f_2a12_43da_9810_300133768217_ctl00_HeadEmployeePart_lbUser ");
              setUserDay( "", cuid.innerHTML);
          });

          function setUserDay(empNo, name) {
              name = encodeURI(name);
               var getEmpUrl =  " http://**/oa/OAInterFace.aspx?flag=due&empid= " + empNo +  " &empname= " + name;
              $. get(getEmpUrl, { Action:  " get " }, function (data, textStatus) {
                   if (textStatus ==  " success ") {
                      ajaxValue = data;
                       // **处理字符串,填充**
                       var arr =  new Array();
                       // ajaxValue = "2010-07-07|2013-07-06|5|00704|XXX|3|5|无限期限"
                      str = ajaxValue.replace(/-/g,  ' / ');
                      arr = str.split( ' | ');
                      $( " input[title='员工姓名'] ").val(arr[ 4]);
                       if (empNo ==  "") {

                          $( " input[title='员工编号'] ").val(arr[ 3]);
                      }
                      $( " input[title='入职日期'] ").val(arr[ 0]);                 
                       var d1 =  new Date(arr[ 0]);
                       var ArrTimeb =  new Array();
                       var ArrTimee =  new Array();
                       var ArrYears =  new Array();
                       if (arr.length >=  8 && arr[ 5] !=  "" && arr[ 6] !=  "" && arr[ 7] !=  "") {
                           // 循环日期,看看当前为几
                           var i =  0;
                           var minindex =  5// 和当前时间最接近的索引
                           var minday =  999999// 日期和当时时间最小的天数
                           var isforver =  false// 是否永久
                           var years =  0;
                           for (i =  5; i <= arr.length; i++) {
                               if (!isNaN(arr[i])) {
                                   var tempday =  new Date(d1.getFullYear() + years, d1.getMonth(), d1.getDate());
                                  ArrTimeb.push(tempday);
                                  ArrYears.push(arr[i]);
                                   // ***********************************************************************

                                  years = years + parseInt(arr[i]);
                                   // ***********************************************************************
                              }
                               else {
                                   // isforver = true;
                                  
// minindex = i;
                                   break;
                              }

                          }

                           var j =  0;
                           for ( var j =  0; j < ArrTimeb.length; j++) {

                               var tempday =  new Date(ArrTimeb[j].getFullYear() + parseInt(ArrYears[j]), ArrTimeb[j].getMonth(), ArrTimeb[j].getDate());
                              tempday.setTime(tempday.getTime() -  1000 *  60 *  60 *  24);
                              ArrTimee.push(tempday);

                               //  alert(ArrTimeb[j].getFullYear() + "==========" + ArrTimee[j].getFullYear() + "==============" + ArrYears[j]);

                          }
                           //  alert(j);

                           var tatime = ArrTimee[ArrTimee.length -  1];

                           var ttime =  new Date(tatime.getFullYear(), tatime.getMonth(), tatime.getDate());

                          ttime.setTime(ttime.getTime() +  1000 *  60 *  60 *  24);

                           // ArrTimeb.push(ttime);
                          ArrTimeb.push(ttime);
                          ArrTimee.push( new Date( 209911));
                          ArrYears.push( 9999);

                           var m =  0;
                           for ( var j =  0; j < ArrTimeb.length; j++) {

                               // 得到两个时间距离当前最近的日期                            
                               var dateNow =  new Date();
                               var nDiff2 = parseInt(dateNow - ArrTimeb[j]) /  3600000 /  24// 第二次的天数
                              
// 取绝对值
                              nDiff2 = Math.abs(nDiff2);
                               // 如果在90天之内,肯定是这个
                              
// m++;
                              
// alert(nDiff2);
                               if (nDiff2 < minday) {
                                  minday = nDiff2;
                                  m = j;
                              }


                               // alert(ArrTimeb[j].getFullYear() + "==========" + ArrTimee[j].getFullYear() + "==============" + ArrYears[j]);
                              
//  alert(ArrTimeb[j].getFullYear());


                          }
                           //  m = m - 1;
                          
//  if(m>)
                          
// alert(m);
                          
// alert(ArrTimeb.length);
                          
//  alert(ArrTimee[m].getFullYear());

                          
// 填充日期*******************************************************************

                           if (m ==  0) {
                               // 当年
                              $( " input[title='劳动合同(年)'] ").val(ArrYears[m]);
                              $( " input[title='起止日期'] ").val(ArrTimeb[m].getFullYear() +  " / " + parseInt(ArrTimeb[m].getMonth() +  1) +  " / " + ArrTimeb[m].getDate());
                              $( " input[title='至'] ").val(ArrTimee[m].getFullYear() +  " / " + parseInt(ArrTimee[m].getMonth() +  1) +  " / " + ArrTimee[m].getDate());

                               // 下一年
                              $( " input[title='续签劳动合同(年)'] ").val(ArrYears[m]);
                              $( " input[title='起止日期2'] ").val(ArrTimeb[m].getFullYear() +  " / " + parseInt(ArrTimeb[m].getMonth() +  1) +  " / " + ArrTimeb[m].getDate());
                              $( " input[title='至2'] ").val(ArrTimee[m].getFullYear() +  " / " + parseInt(ArrTimee[m].getMonth() +  1) +  " / " + ArrTimee[m].getDate());

                               // 无固定期按钮不要点上
                               if ($( " input[title='无固定期'] ").attr( " checked ")) {
                                  $( " input[title='无固定期'] ").click();
                              }
                          }
                           else {
                               // 当年
                              $( " input[title='劳动合同(年)'] ").val(ArrYears[m -  1]);
                              $( " input[title='起止日期'] ").val(ArrTimeb[m -  1].getFullYear() +  " / " + parseInt(ArrTimeb[m -  1].getMonth() +  1) +  " / " + ArrTimeb[m -  1].getDate());
                              $( " input[title='至'] ").val(ArrTimee[m -  1].getFullYear() +  " / " + parseInt(ArrTimee[m -  1].getMonth() +  1) +  " / " + ArrTimee[m -  1].getDate());

                               // 下一年
                              $( " input[title='续签劳动合同(年)'] ").val(ArrYears[m]);
                              $( " input[title='起止日期2'] ").val(ArrTimeb[m].getFullYear() +  " / " + parseInt(ArrTimeb[m].getMonth() +  1) +  " / " + ArrTimeb[m].getDate());
                              $( " input[title='至2'] ").val(ArrTimee[m].getFullYear() +  " / " + parseInt(ArrTimee[m].getMonth() +  1) +  " / " + ArrTimee[m].getDate());

                               // 无固定期按钮不要点上
                               if ($( " input[title='无固定期'] ").attr( " checked ")) {
                                  $( " input[title='无固定期'] ").click();
                              }


                          }

                           if ($( " input[title='续签劳动合同(年)'] ").val() ==  " 9999 ") {

                              $( " input[title='续签劳动合同(年)'] ").val( " 无固定期 ");
                               if (!$( " input[title='无固定期'] ").attr( " checked ")) {
                                  $( " input[title='无固定期'] ").click();
                              }
                              $( " input[title='至2'] ").val( " 2099/01/01 ");
                          }

                           // end填充日期*******************************************************************

                           return;



                           var bd1 =  new Date(arr[ 5]);
                           var bd2 =  new Date(arr[ 6]);
                           var bd3 =  new Date(arr[ 7]);


                           // 得到两个时间距离当前最近的日期
                           var dateNow =  new Date();
                           var nDiff2 = parseInt(dateNow - bd2) /  3600000 /  24// 第二次的天数
                           var nDiff3 = parseInt(dateNow - bd3) /  3600000 /  24// 第三次的天数


                          
// 取绝对值
                          nDiff2 = Math.abs(nDiff2);
                          nDiff3 = Math.abs(nDiff3);
                           // 如果第二次的离当前的日期近
                           if (nDiff2 < nDiff3) {
                               // 第二次签合同

                              
// 当年
                              $( " input[title='劳动合同(年)'] ").val(Math.floor(parseInt(bd2 - bd1) /  3600000 /  24 /  365));
                              $( " input[title='起止日期'] ").val(bd1.getFullYear() +  " / " + bd1.getMonth() +  " / " + bd1.getDate());
                              $( " input[title='至'] ").val(bd2.getFullYear() +  " / " + bd2.getMonth() +  " / " + bd2.getDate() -  1);

                               // 下一年
                              $( " input[title='续签劳动合同(年)'] ").val(Math.floor(parseInt(bd3 - bd2) /  3600000 /  24 /  365));
                              $( " input[title='起止日期2'] ").val(bd2.getFullYear() +  " / " + bd2.getMonth() +  " / " + bd2.getDate());
                              $( " input[title='至2'] ").val(bd3.getFullYear() +  " / " + bd3.getMonth() +  " / " + bd3.getDate() -  1);

                               // 无固定期按钮不要点上
                               if ($( " input[title='无固定期'] ").attr( " checked ")) {
                                  $( " input[title='无固定期'] ").click();
                              }


                          }
                           else {
                               // 第三次签合同
                              
// 当年
                              $( " input[title='劳动合同(年)'] ").val(Math.floor(parseInt(bd3 - bd2) /  3600000 /  24 /  365));
                              $( " input[title='起止日期'] ").val(bd2.getFullYear() +  " / " + bd2.getMonth() +  " / " + bd2.getDate());
                              $( " input[title='至'] ").val(bd3.getFullYear() +  " / " + bd3.getMonth() +  " / " + bd3.getDate() -  1);

                               // 下一年
                              $( " input[title='续签劳动合同(年)'] ").val( " 无固定期 ");
                              $( " input[title='起止日期2'] ").val(bd3.getFullYear() +  " / " + bd3.getMonth() +  " / " + bd3.getDate());
                              $( " input[title='至2'] ").val( " 2099/01/01 ");

                               // 无固定期按钮不要点上
                               if (!$( " input[title='无固定期'] ").attr( " checked ")) {
                                  $( " input[title='无固定期'] ").click();
                              }

                          }



                      }

                       //

                      
//                       var d1 = new Date(arr[0]);  
                      
//                       var d2 = new Date(arr[1]);
                      
//
                      
// two                    
                      
//                       if ((arr.length > 3) && (arr[1] != "")) {
                      
//

                      
//                           var d3 = new Date(d2.getFullYear(), d2.getMonth() + 1, d2.getDate() + 1);

                      
//                           $("input[title='起止日期2']").val(d3.getFullYear() + "/" + d3.getMonth() + "/" + d3.getDate());
                      
//                           if (arr[2] == 5) {
                      
//                               $("input[title='续签劳动合同(年)']").val("无固定期");
                      
//                               $("input[title='至2']").val("2999/01/01");

                      
//                                // 无固定期按钮点上
                      
//                               if (!$("input[title='无固定期']").attr("checked")) {
                      
//                                   $("input[title='无固定期']").click();
                      
//                               }


                      
//                           }
                      
//                           else if (arr[2] == 3) {
                      
//                               $("input[title='续签劳动合同(年)']").val("5");
                      
//                               var d4 = new Date(d3.getFullYear() + 5, d3.getMonth(), d3.getDate() - 1);
                      
//                               $("input[title='至2']").val(d4.getFullYear() + "/" + d4.getMonth() + "/" + d4.getDate());

                      
//                                // 无固定期按钮点上
                      
//                               if ($("input[title='无固定期']").attr("checked")) {
                      
//                                   $("input[title='无固定期']").click();
                      
//                               }

                      
//                           }
                      
//                           else {
                      
//                               if ($("input[title='无固定期']").attr("checked")) {
                      
//                                   $("input[title='无固定期']").click();
                      
//                               }

                      
//                               $("input[title='续签劳动合同(年)']").val("3");
                      
//                               var d4 = new Date(d3.getFullYear() + 3, d3.getMonth(), d3.getDate() - 1);
                      
//                               $("input[title='至2']").val(d4.getFullYear() + "/" + d4.getMonth() + "/" + d4.getDate());
                      
//                           }

                      
//                       }



                      
// alert(ajaxValue);
                  }
              });

          
          }





           // ************************************************


         
         
// *************************************************** 
         
      
      </script>

你可能感兴趣的:(时期日期相关JS)