I. Time to week
This principle is relatively simple. Find the day of the week of the current week and the day of the month, then subtract, divide by the number of milliseconds of the week, and then add 1. The Code is as follows:
// Time to week: parameter (timestamp) function time2week (time) {var current = new date (time); var current_year = current. getfullyear (); var current_month = current. getmonth (); var current_date = current. getdate (); var current_week = current. getday (); var Thisyear = new date (current_year, 0, 1); // var thisyear_week = Thisyear on the first day of the year. getday (); var thisweek = new date (current_year, current_month, thisyear_week-current_week + current_date); // var cut = thisweek-Thisyear on the first day of the year; vaR week = cut/(1000*60*60*24*7) + 1; return week;} var timeline = 1410249135; alert (time2week (timeline * 1000 )); // 37Ii. Weekly Data Transfer Time
This is a little more complex than above. First, determine the day of the first day of the year and the day of the second week of the year (because the Monday of the first week may be in the previous year ), determine the day of the week based on the number of weeks. The Code is as follows:
// Week conversion time: parameter (which week of the year, such as 201437) function week2date (STR) {STR = Str. tostring (); var res ={}; var year = Str. substring (0, 4); var week = Str. substring (4); var Thisyear = new date (year, 0, 1); var thisyear_week = Thisyear. getday (); // obtain Thisyear of week Week. setdate (parseint ("2876543 ". charat (thisyear_week) + (week-2) * 7); Res. start = {year: Thisyear. getfullyear (), month: Thisyear. getmonth () + 1, data: Thisyear. getdate ()}; // obtain Thisyear of the week Week. setdate (Thisyear. getdate () + 6); Res. end = {year: Thisyear. getfullyear (), month: Thisyear. getmonth () + 1, data: Thisyear. getdate ()}; return res;} console. log (week2date (201437); // object {start: object, end: Object} // start: Object/data: 8 // month: 9 // year: 2014 // end: Object // data: 14 // month: 9/year: 2014
Weekly Data Processing