When JS gets the date, you encounter the following requirements, and obtain the date of one week according to a certain year. If the start date is specified as Thursday to Friday of the next week, it is one week.
The code is as follows:
function getNowFormatDate(theDate) { var day = theDate; var Year = 0; var Month = 0; var Day = 0; var CurrentDate = ""; // Initialization time Year= day.getFullYear();// ie can be used under Firefox Month= day.getMonth()+1; Day = day.getDate(); CurrentDate += Year + "-"; if (Month >= 10 ) { CurrentDate += Month + "-"; } else { CurrentDate += "0" + Month + "-"; } if (Day >= 10 ) { CurrentDate += Day ; } else { CurrentDate += "0" + Day ; } return CurrentDate; } function isInOneYear(_year,_week){ if(_year == null || _year == '' || _week == null || _week == ''){ return true; } var theYear = getXDate(_year,_week,4).getFullYear(); if(theYear != _year){ return false; } return true; } // Get the date range display function getDateRange(_year,_week){ var beginDate; var endDate; if(_year == null || _year == '' || _week == null || _week == ''){ return ""; } beginDate = getXDate(_year,_week,4); endDate = getXDate(_year,(_week - 0 + 1),5); return getNowFormatDate(beginDate) + " to " + getNowFormatDate(endDate); } // This method will getXDate(year,weeks,weekDay){ // Construct a date object with the specified year and set the date to January 1 of the year// Because the month in the computer starts from 0, there is the following construction method var date = new Date(year,"0","1"); // Get the long plastic surgery time of the date object date time var time = date.getTime(); // Add this long plastic surgery time to the N-week time// Because the first week is the current week, there is: weeks-1, and so on/ 7*24*3600000 is the number of time milliseconds in a week, (the date in JS is accurate to milliseconds) time+=(weeks-1)*7*24*3600000; // Reset the date object date to time time date.setTime(time); return getNextDate(date,weekDay); } // This method will get the date of the week (weekDay) of a certain date (nowDate) function getNextDate(nowDate,weekDay){ // 0 is Sunday, 1 is Monday,... weekDay%=7; var day = nowDate.getDay(); var time = nowDate.getTime(); var sub = weekDay-day; if(sub <= 0){ sub += 7; } time+=sub*24*3600000; nowDate.setTime(time); return nowDate; }If you obtain the first week date of 2016, it will start counting on Thursday. Then the date range for the first week is 2016-01-07 to 2016-01-15
In the reference code for providing a call:
//Date processing function dateRange(){ var _year = $("#_year").val(); var _week = $("#_week").val(); if(isInOneYear(_year,_week)){ var showDate = getDateRange(_year,_week); $("#_dateRange_import").html(showDate); } else{ alert(_year+"year none"+_week+"week+"week, please reselect"); $("#_week").val(""); } }