Js获取当前日期时间、比较及正则表达式转换格式

来源:互联网 发布:软件项目总结报告ppt 编辑:程序博客网 时间:2024/05/20 07:49

获取当前日期时间

var now = new Date();
now.getYear();        //获取当前年份(2位)
now.getFullYear();    //获取完整的年份(4位,1970-****)
now.getMonth();       //获取当前月份(0-11,0代表1月)
now.getDate();        //获取当前日(1-31)
now.getDay();         //获取当前星期X(0-6,0代表星期天)
now.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
now.getHours();       //获取当前小时数(0-23)
now.getMinutes();     //获取当前分钟数(0-59)
now.getSeconds();     //获取当前秒数(0-59)
now.getMilliseconds();    //获取当前毫秒数(0-999)
now.toLocaleDateString();     //获取当前日期
now.toLocaleTimeString();     //获取当前时间
now.toLocaleString( );        //获取日期与时间

//日期时间比较

function getBuXiu()

 var Judge = true;
 var stateCaption = getFormElement('_STATECAPTION');
 if(stateCaption==null || stateCaption.value=="")
 { 
  var QjStart = document.getElementsByName("QjStart")[0];
  var QjEnd = document.getElementsByName("QjEnd")[0];
  var QjDays = document.getElementsByName("QjDays")[0];   
  if((QjStart.value!="")&&(QjEnd.value!=""))
  {
   if((new Date(QjStart.value.replace(/-/g,"\/"))) >= (new Date(QjEnd.value.replace(/-/g,"\/"))))//日期时间比较及JS 正则表达式替换所有
   {
    Judge = false;
    alert('开始时间大于或等于结束时间,请检查!');    
   }
   else
   {
    QjDays.value=DateDiff(QjStart.value,QjEnd.value);//赋值
   }
  }
 }
 return Judge; 
}

//日期时间格式转换
function DateDiff(sDate1, sDate2) {
 var arrDate, objDate1, objDate2, intDays;
 arrDate = sDate1.split('-');
 objDate1 = new Date(arrDate[1] + '-' + arrDate[2] + '-' + arrDate[0]);
 arrDate = sDate2.split('-');
 objDate2 = new Date(arrDate[1] + '-' + arrDate[2] + '-' + arrDate[0]);
        var hours=parseInt(Math.floor(objDate2 - objDate1)/(3600*1000));
        if(hours==0)
        intDays = 0;
        else if(((hours/8)<1)&&((hours/24)<1))
        intDays = Math.abs(hours/8); 
        else if(((hours/8)>=1)&&((hours/24)<=1))
        intDays = 1;
        else if(((hours/24)>1)&&((hours%24)<8)&&((hours%24)>0))
        intDays = (parseInt((hours/24))+Math.abs((hours%24)/8));//转换成数字类型  及数的绝对值

        else if(((hours/24)>1)&&((hours%24)>=8)&&((hours%24)<24))
        intDays = (parseInt(hours/24)+1);  
 //intDays = parseInt(Math.abs(objDate1 - objDate2) / 1000 / 60 / 60 / 24);
 return intDays;
}

0 0
原创粉丝点击