常用函数备用
1、返回两个时间段相差的天数
function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2002-12-18格式,参数类型是字符串
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2002格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数
return iDays
}
2、根据天数,计算出来是几月几号,只能计算一年内的
// yearNu: 年份 number
// Days: 天数 number
// calculateDates(2008, 344); {year: 2008, month: 12, day: "09"}
function calculateDates(yearNu, Days) {
let isLeap = (0 == yearNu % 4);
let days = isLeap ? 366 : 365;
let feb = days == 365 ? 28 : 29;
let count = 0;
var curMonth = '';
var curday = '';
var objDate = {};
let a = 1,
b = 1,
c = 1,
d = 1,
e = 1,
f = 1,
g = 1,
h = 1,
ii = 1,
j = 1,
k = 1,
l = 1;
for (let i = 1; i <= curDays; i++) {
// 每个月的第一天
if (i == 1 || // 1月
i == 31 + 1 || // 2
i == 31 + feb + 1 || //3
i == 31 * 2 + feb + 1 || // 4
i == 31 * 2 + feb + 30 + 1 || // 5
i == 31 * 3 + feb + 30 + 1 || // 6
i == 31 * 3 + feb + 30 * 2 + 1 || // 7
i == 31 * 4 + feb + 30 * 2 + 1 || // 8
i == 31 * 5 + feb + 30 * 2 + 1 || // 9
i == 31 * 5 + feb + 30 * 3 + 1 || // 10
i == 31 * 6 + feb + 30 * 3 + 1 || // 11
i == 31 * 6 + feb + 30 * 4 + 1 // 12
) {
count++;
curMonth = count;
curday = 1;
} else { // 其余天数
if (i > 1 && i < 31 + 1) {
a++;
curMonth = count;
curday = a;
} else if (i > 31 + 1 && i < 31 + feb + 1) {
b++;
curday = b;
curMonth = count;
} else if (i > 31 + feb + 1 && i < 31 * 2 + feb + 1) {
c++;
curday = c;
curMonth = count;
} else if (i > 31 * 2 + feb + 1 && i < 31 * 2 + feb + 30 + 1) {
d++;
curday = d;
curMonth = count;
} else if (i > 31 * 2 + feb + 30 + 1 && i < 31 * 3 + feb + 30 + 1) {
e++;
curday = e;
curMonth = count;
} else if (i > 31 * 3 + feb + 30 + 1 && i < 31 * 3 + feb + 30 * 2 + 1) {
f++;
curday = f;
curMonth = count;
} else if (i > 31 * 3 + feb + 30 * 2 + 1 && i < 31 * 4 + feb + 30 * 2 + 1) {
g++;
curday = g;
curMonth = count;
} else if (i > 31 * 4 + feb + 30 * 2 + 1 && i < 31 * 5 + feb + 30 * 2 + 1) {
h++;
curday = h;
curMonth = count;
} else if (i > 31 * 5 + feb + 30 * 2 + 1 && i < 31 * 5 + feb + 30 * 3 + 1) {
ii++;
curday = ii;
curMonth = count;
} else if (i > 31 * 5 + feb + 30 * 3 + 1 && i < 31 * 6 + feb + 30 * 3 + 1) {
j++;
curday = j;
curMonth = count;
} else if (i > 31 * 6 + feb + 30 * 3 + 1 && i < 31 * 6 + feb + 30 * 4 + 1) {
k++;
curday = k;
curMonth = count;
} else if (i > 31 * 6 + feb + 30 * 4 + 1) {
l++;
curday = l;
curMonth = count;
}
}
}
if (curMonth < 10) {
curMonth = '0' + curMonth;
}
if (curday < 10) {
curday = '0' + curday;
}
if (curMonth == 0) {
curMonth = '0' + 1;
}
if (curday == 0) {
curday = '0' + 1;
}
objDate.year = yearNu;
objDate.month = curMonth;
objDate.day = curday;
return objDate;
}
3.获取浏览器网址,切割的等操作。
var getWebRootPath = function () {
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPath = curWwwPath.substring(0, pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return {
localhostPath: localhostPath,
projectName: projectName
};
};