1. 程式人生 > >獲取當前系統時間兩小時後和四小時後,蘋果瀏覽器報錯NaN,或者Invalid Date,

獲取當前系統時間兩小時後和四小時後,蘋果瀏覽器報錯NaN,或者Invalid Date,

    <script type="text/javascript" src="js/libs/jquery.min.js"></script>

   <script type="text/javascript" src="js/xdate.js"></script>


/**獲取當前系統兩小時後的時間*/

function  orderStartdate(){
/**

* 獲取當前時間
*/
function p(s) {
   return s < 10 ? '0' + s: s;
}


var myDate = new Date();
//獲取當前年
var year=myDate.getFullYear();
//獲取當前月
var month=myDate.getMonth()+1;
//獲取當前日
var date=myDate.getDate(); 
var h=myDate.getHours();       //獲取當前小時數(0-23)
var m=myDate.getMinutes();     //獲取當前分鐘數(0-59)
var s=myDate.getSeconds();  
var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);//當前系統時間
$('#now').val(now);
var twoTime=new XDate(now).getTime()+ 1*2*60*60*1000 + 1*0.25*60*60*1000;//當前系統兩小時候後的時間
$('#orderStartdate').val(getMyDate(twoTime));
}

/**獲取當前系統四小時後的時間*/
function orderEnddate(){
/**

* 獲取當前時間
*/
function p(s) {
   return s < 10 ? '0' + s: s;
}


var myDate = new Date();
//獲取當前年
var year=myDate.getFullYear();
//獲取當前月
var month=myDate.getMonth()+1;
//獲取當前日
var date=myDate.getDate(); 
var h=myDate.getHours();       //獲取當前小時數(0-23)
var m=myDate.getMinutes();     //獲取當前分鐘數(0-59)
var s=myDate.getSeconds();  
var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s); //當前系統時間
var d = new XDate(now).getTime() + 1*2*60*60*1000 + 1*0.25*60*60*1000;
var four = d +1*2*60*60*1000;
$('#orderenddate').val(getMyDate(four));
}

  function getMyDate(str){  
         var oDate = new Date(str),  
         oYear = oDate.getFullYear(),  
         oMonth = oDate.getMonth()+1,  
         oDay = oDate.getDate(),  
         oHour = oDate.getHours(),  
         oMin = oDate.getMinutes(),  
         oSen = oDate.getSeconds(),  
         oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最後拼接時間  
         return oTime;  
     }; 
     //補0操作
   function getzf(num){  
       if(parseInt(num) < 10){  
           num = '0'+num;  
       }  
       return num;  
   }