Baidu applet will display the time stamp into a few days ago, a few hours ago, and loop rendering to the view layer

Poor Baidu small program, few documents, few examples, online can not find... I found a lot of useless ones. Alas, I have to think about it myself

I. first of all:

When the data is obtained (a pile of array data returned by the server), the time in the data is converted, and then output to the logical layer data, and then the view layer is automatically updated.

II. Code above:

    success: res=> {    
             var arr = res.data;
            for (var i = 0, len = arr.length; i < len; i++) {
            console.log(arr[i].time);//Ergodic output
            arr[i].time = this.getDateDiff(arr[i].time);//Knot new value
            }
        console.log(res.data);
        if(res.data==null){
            return
        }
       this.setData({
          carData: this.data.carData.concat(res.data),//carData is the array KEY in your page, which can be changed by yourself
          }); 
       console.log(res.data);
        console.log(res.data[0].id);       
        swan.stopPullDownRefresh();
    },

Then there's the time shift:

//Time conversion
    getDateDiff:function(e) {
		    //Convert string to time format
		    var timePublish = new Date(e);
		    var timeNow = new Date();
		    var minute = 1000 * 60;
		    var hour = minute * 60;
		    var day = hour * 24;
		    var month = day * 30;
		    var diffValue = timeNow - timePublish;
		    var diffMonth = diffValue / month;
		    var diffWeek = diffValue / (7 * day);
		    var diffDay = diffValue / day;
		    var diffHour = diffValue / hour;
		    var diffMinute = diffValue / minute;
		  console.log(diffMonth);
		    if (diffValue < 0) {
		//      alert("error time");
		    }
		//  else if (diffMonth > 3) {
		//      result = timePublish.getFullYear() + "-";
		//      result += timePublish.getMonth() + "-";
		//      result += timePublish.getDate();
		////      alert(result);
		//  }//More than three days display specific time
		    else if (diffMonth > 1) {
		        result = parseInt(diffMonth) + "Months ago";
		    }
		    else if (diffWeek > 1) {
		        result = parseInt(diffWeek) + "Zhou Qian";
		    }
		    else if (diffDay > 1) {
		        result = parseInt(diffDay) + "Days ago";
		    }
		    else if (diffHour > 1) {
		        result = parseInt(diffHour) + "Hours ago";
		    }
		    else if (diffMinute > 1) {
		        result = parseInt(diffMinute) + "Minutes ago";
		    }
		    else {
		        result = "Just released";
		    }
		    return result;
		}

So far it's done.

Tags: Front-end

Posted on Wed, 04 Dec 2019 07:00:47 -0800 by renegade888