Work encountered a requirement to change its state for the "distance" from the specified time to the current time. This requirement is done out, packaged, can be used to pass the parameter. /*create By suji 2018-09-07*/ / determine what day of the week is returnedfunction week(dayNum){
	    	switch(dayNum){
	    		case 7:
	    		return 'day';
	    		break;
	    		case 1:
	    		return '一';
	    		break;
	    		case 2:
	    		return '二';
	    		break;
	    		case 3:
	    		return '三';
	    		break;
	    		case 4:
	    		return 'four';
	    		break;
	    		case 5:
	    		return 'five';
	    		break;
	    		case 6:
	    		return '六';
	    		break; }} // Timestamp format parameter time: the specified time, nowtime: the current timefunctiontimeTo(time,nowtime) { var date = new Date(time); var nowdate =new Date(nowtime); var PY = date.getFullYear(); Var NY = nowdate.getFullYear(); Var Y = date.getFullYear() +'/';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '/';
        var D = date.getDate() + ' ';
        var h = date.getHours() + '/';
        var m = date.getMinutes() + '/';
        var s = date.getSeconds();
        if(PY==NY){// Check whether the year is the same, yes output format 08/01 if not 2018/08/01return M+D;
        }
        else{
        	returnY+M+D; }} // Convert the timestamp to the same time xxxx-xx-xx 00:00:00function timeTotime(time) {
        var date = new Date(time);
   		var Y = date.getFullYear() + The '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + The '-';
        var D = date.getDate() + ' ';
        var h = 00 + ':';
        var m = 00 + ':';
        var s = 00;
        var newtime= Y+M+D+h+m+s;
        returnnew Date(newtime).getTime(); If it is the same day, today is displayed; if it is less than one day, yesterday is displayed; if it is more than two days, the day of the week is displayed; if it is more than one week, the day of the week is displayed; if it is more than one week, the day of the week is displayed; if it is more than one week, the day of the week is displayed. Tomorrow, the day after, // same thing next week.ifDate(1536600501000); / / the refsfunction ifDate(perDate){
		var date = new Date().getTime();//获取当前时间时间戳
		var date = timeTotime(date);//对时间戳进行转化,转化成时间统一xxxx-xx-xx 00:00:00
		var perDate =timeTotime(perDate);
		console.log(date,perDate);
		var runTime =(date-perDate)/1000;
		var year = Math.floor(runTime / 86400 / 365);
		    runTime = runTime % (86400 * 365);
		    var month = Math.floor(runTime / 86400 / 30);
		    runTime = runTime % (86400 * 30);
		    var day = Math.floor(runTime / 86400);
		    var day =parseInt(day);//获取相差的天数,可以为负数
		    console.log(day);
		    var nowday = new Date(date).getDay();//获取当前时间是星期几,0-6
		    if(nowday==0){// If 0, set Sunday to 7. }if(day==0){
		    	console.log('today');
		    }
		    else if(day==1){
		    	console.log('yesterday');
		    }
		    else if(day==2){
		    	console.log('the day before yesterday);
		    }
		    else if(day==-1){
		    	console.log('tomorrow');
		    }
		    else if(day==-2){
		    	console.log('the day after tomorrow');
		    }
		    else ifConsole. log(day,nowday); (day>2&&day<nowday){console.log(day,nowday); var dayNum =nowday-day; console.log('week'+(week(dayNum)));
		    }
		    else if(day>2&&day>=nowday&&day<nowday+7){var dayNum=7- math.abs ((nowday-day)); console.log('last week'+(week(dayNum)));
		    }
		    else if(day>= nowDay +7){console.log(timeTo(perDate,date))}else if(math.abs (day)<(7-nowday)&&day<0){var dayNum = math.abs (day)+nowday; console.log('week'+(week(dayNum)));
		    }
		    else if(Math.abs(day)>(7-nowday)&&Math.abs(day)<(7-nowday+7)&&day<0){ var dayNum=Math.abs(day)+nowday-7; // Check whether it is next week console.log('next week'+(week(dayNum)));
		    }
		    else if(math.abs (day)>(7-nowday+7)&&day<0){// Check whether the next week is over, display the normal format console.log(timeTo(perDate,date)); }}Copy the code

This function feels more practical, we can take useful to use, of course, may need to optimize, but also ask the gods to change their own manual change.