摘要:周日使用getDay()获得的是0,getDate()获取的月份是0-11,需要做处理
var nowTemp = new Date(),
oneDayLong = 24*60*60*1000,
c_time = nowTemp.getTime(),
c_day = (nowTemp.getDay()||7)-7,
m_time = c_time - (c_day-1)*oneDayLong,
monday = new Date(m_time),
m_year = monday.getFullYear(),
m_month = monday.getMonth()+1,
m_date = monday.getDate(),
nextMondayTime = m_year + '-' + m_month + '-' + m_date, // 下周一日期
currentTime = nowTemp.getFullYear() + '-' + (nowTemp.getMonth()+1) + '-' + nowTemp.getDate(); //当前日期
function getDifferDay(down,up){
var sdate = new Date(down.replace(/-/g,'/')),//替换- 兼容ios
now = new Date(up.replace(/-/g,'/')),
days = now.getTime() - sdate.getTime(),
day = parseInt(days / (1000 * 60 * 60 * 24));
return day < 0 ? 0 : day;
}
console.log(currentTime,nextMondayTime,getDifferDay(currentTime,nextMondayTime));