1、获取当前时间 - 精确到秒(本机时间)
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minute >= 0 && minute <= 9) {
minute = "0" + minute;
}
if (second >= 0 && second <= 9) {
second = "0" + second;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + hour + seperator2 + minute + seperator2 + second;
return currentdate;
};
2、获取一个月前时间- 精确到秒(本机时间)
function getPreFormatDate(date) {
var seperator1 = "-";
var seperator2 = ":";
var year = date.substring(0, 4);
var month = date.substring(5, 7);
var day = date.substring(8, 10);
var hour = date.substring(11, 13);
var minute = date.substring(14, 16);
var second = date.substring(17, 19);
var nextYear = year;
var nextMonth = parseInt(month) - 1;
if(nextMonth == 0) {
nextYear = parseInt(nextYear) - 1;
nextMonth = 12;
}
var nextDay = day;
var days2 = new Date(nextYear, nextMonth, 0);
days2 = days2.getDate();
if(nextDay > days2) {
nextDay = days2;
}
if(nextMonth < 10) {
nextMonth = '0' + nextMonth;
}
var t2 = nextYear + seperator1 + nextMonth + seperator1 + nextDay + " " + hour + seperator2 + minute + seperator2 + second;
return t2;
}
3、时间大小比较 及 时间间隔判断(不超过一个月)
function timeCompare(startDateTime, endDateTime) {
// 对开始时间进行处理
var startYear = startDateTime.substring(0, 4);
var startMonth = startDateTime.substring(5, 7);
var startDate = startDateTime.substring(8, 10);
var startHour = startDateTime.substring(11, 13);
var startMin = startDateTime.substring(14, 16);
var startSecond = startDateTime.substring(17, 19);
var sTime = new Date(startYear, startMonth, startDate,startHour,startMin,startSecond);
var startTime = sTime.getTime();
// 对结束时间进行处理
var endYear = endDateTime.substring(0, 4);
var endMonth = endDateTime.substring(5, 7);
var endDate = endDateTime.substring(8, 10);
var endHour = endDateTime.substring(11, 13);
var endMin = endDateTime.substring(14, 16);
var endSecond = endDateTime.substring(17, 19);
var eTime = new Date(endYear, endMonth, endDate,endHour,endMin,endSecond);
var endTime = eTime.getTime();
// 计算相差多少个月
var len = (endYear - startYear) * 12 + (endMonth - startMonth);
var monthInterval = ((len * 12 + endMonth) - startMonth);
var dateInterval = (endDate - startDate);
// 31 天的月份
var eapArr = new Array(1, 3, 5, 7, 8, 10, 12);
var bool = contains(eapArr, parseInt(endMonth));
if (startTime > endTime) {
alert("开始日期不能大于结束日期!");
return false;
} else if (len > 1
|| (monthInterval >= 1 && dateInterval >= 1 && bool == true)) {
alert("日期间隔不能超过一个月!");
return false;
} else if (len > 1
|| (monthInterval >= 1 && dateInterval > 0 && bool == false)) {
alert("日期间隔不能超过一个月!");
return false;
} else {
return true;
}
}
// 判斷元素是否在數組中
function contains(arr, obj) {
var i = arr.length;
while (i--) {
if (arr[i] === obj) {
return true;
}
}
return false;
}