计算天数差、获取当前时间、获取百分比

//计算天数差
function getDayNumber(date1, date2) {
//默认格式为"20030303",根据自己需要改格式和方法
var year1 = date1.substr(0, 4);
var year2 = date2.substr(0, 4);
var month1 = date1.substr(4, 2);
var month2 = date2.substr(4, 2);

var day1 = date1.substr(6, 2);
var day2 = date2.substr(6, 2);

temp1 = year1 + "/" + month1 + "/" + day1;
temp2 = year2 + "/" + month2 + "/" + day2;

var dateaa = new Date(temp1);
var datebb = new Date(temp2);
var date = datebb.getTime() - dateaa.getTime();
var time = Math.floor(date / (1000 * 60 * 60 * 24));
return time;
}

//获取当前时间
function getNowFormatDate() {
var date = new Date();
var seperator1 = ".";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
return currentdate;
}

//获取百分比
function getPercent(num, total) {
num = parseFloat(num);
total = parseFloat(total);
if (isNaN(num) || isNaN(total)) {
return "-";
}
return total <= 0 ? "0%" : (Math.round(num / total * 10000 / 100) + "%");
}

posted @ 2016-07-20 14:46  开心的饭饭  阅读(492)  评论(0编辑  收藏  举报