function DateTime(Tid) { var now = new Date(); var validDate = "", day = now.getDate(), month = now.getMonth(), year = now.getFullYear(); var nowTime = now.getTime(); var oneDayTime = 24 * 60 * 60 * 1000; var mDay = now.getDay(); /*显示周一*/ var MondayTime = nowTime - (mDay - 1) * oneDayTime; /*显示周日*/ var SundayTime = nowTime + (7 - mDay) * oneDayTime; /*初始化日期时间*/ var monday = new Date(MondayTime); var sunday = new Date(SundayTime); switch (Tid) { case "1": validDate = year + '.' + (month + 1) + '.' + day; break; case "2": validDate = year + '.' + (month + 1) + '.' + (day + 1); break; case "3": validDate = monday.getFullYear() + '.' + (monday.getMonth() + 1) + '.' + monday.getDate() + '~' + sunday.getFullYear() + '.' + (sunday.getMonth() + 1) + '.' + sunday.getDate(); break; case "4": var tempdate = year + '.' + (month + 1), enddate = getCurrentMonthLast(new Date(tempdate)); validDate = year + '.' + (month + 1) + '.1~' + tempdate + '.' + enddate; break; } if (validDate) { validDate = '有效期限:' + validDate; $('.js-validdate').text(validDate); } function getCurrentMonthLast(date) { var currentMonth = date.getMonth(); var nextMonth = ++currentMonth; var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1); var oneDay = 1000 * 60 * 60 * 24; return new Date(nextMonthFirstDay - oneDay).getDate(); } }
Tid是点击今天时间、明天时间、本周时间、本月时间的id