获取两个时间之间的日期
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>计算两日期时间相差多少天</title>
<script>
function getTime2Time($time1, $time2)
{
var time1 = arguments[0], time2 = arguments[1];
time1 = Date.parse(time1)/1000;
time2 = Date.parse(time2)/1000;
var time_ = time1 - time2;
return (time_/(3600*24));
}
document.write(getTime2Time('2016-08-02', '2017-07-02'));
var Computation =function(sDate1, sDate2){ //sDate1和sDate2是2008-12-13格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-13-2008格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
//alert(iDays);
return iDays
}
document.write("\t"+Computation('2016-08-02', '2017-07-02')+"天");
function getDays(DateOne,DateTwo){
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
return Math.abs(cha);
}
document.write("\t"+getDays('2016-08-02', '2017-09-02')+"天");
</script>
</head>
<body>
</body>
</html>
百分比计算
formatTodata:function (row, column) {
var time1=(!row.contracttime || row.contracttime == '') ? '' : util.formatDate.format(new Date(row.contracttime), 'yyyy-MM-dd')
var time2=(!row.contracttimeschedule || row.contracttimeschedule == '') ? '' : util.formatDate.format(new Date(row.contracttimeschedule), 'yyyy-MM-dd');
var aDate, oDate1, oDate2, iDays,num,total;
aDate = time1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-13-2008格式
aDate = time2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
num = parseFloat(iDays);
total = parseFloat(365);
if (isNaN(num) || isNaN(total)) {
return "-";
}
return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00 + "%");
},