根据日期判断是否在当前月
function SameMonth(date) {
var date1 = new Date(date.replace(/-/g, "/")); //将传入的时间字符串转换成时间对象
var date2 = new Date(); //当前时间
var curDay = date2.getDate(); //获取当前几号
var firstDay = GetDate((curDay), 1); //计算出当月第一天
var lastDay = GetDate((getDaysInMonth(date2.getFullYear(), date2.getMonth() + 1) - curDay), 2); //计算出当月最后一天
if (date1.getTime() < firstDay.getTime() || date1.getTime() > lastDay.getTime()) {
return false; //不在同一个月内
} else {
return true; //在同一个月内
}
}
function getDaysInMonth(year, month) {
month = parseInt(month, 10) + 1;
var temp = new Date(year + "/" + month + "/0");
return temp.getDate();
}
/*
*获取当前日期前N天或后N日期(N = day)
*type:1:前;2:后
*/
function GetDate(day, type) {
var zdate = new Date();
var edate;
if (type == 1) {
edate = new Date(zdate.getTime() - (day * 24 * 60 * 60 * 1000));
} else {
edate = new Date(zdate.getTime() + (day * 24 * 60 * 60 * 1000));
}
return edate;
}