When JS obtains a date, it encounters the following requirement: Obtain the date of a week based on a week of a year. For example, the start date must be from Thursday to Friday of the next 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 (); // you can set Month = day under ie Firefox. getMonth () + 1; Day = day. getDate (); CurrentDate + = Year + "-"; if (Month> = 10) {CurrentDate + = Month + "-";} else {CurrentDate + = "0" + Month + "-";} if (Day >=10) {CurrentDate + = Day;} else {CurrentDate + = "0" + D Ay;} 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;} // obtain the 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 gets the weekDay of the week (weeks) of a year (year) date function 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, the following constructor is available: var Date = new date (year, "0 ", "1"); // get the long integer time of the date object time var time = date. getTime (); // offset the long integer time plus the time of week N. // because the first week is the current week, there are: weeks-1, and so on // 7*24*3600000 is the number of milliseconds in a week, (the date in JS is accurate to milliseconds) time + = (weeks-1) * 7*24*3600000; // reset the date object to the time date. setTime (time); return getNextDate (date, weekDay) ;}// this method will get the date 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 ;}
For example, if you get the date of the first week of January 1, 2016, it will start from Thursday. The date range of the first week is
Provide reference code for 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" pai.html (showDate);} else {alert (_ year + "" + _ week + ", please reselect "); $ ("# _ week "). val ("");}}