小程序更改时间为某时/天/月/年前格式

				var  time = [ ' 2019-02-01 ' ]
				 function getDateAfter(ev) {
                      var addt = time; //获取之前时间       
                      var years = new Date(addt).getTime(); //转成时间戳格式
                      var result;
                      var minute = 1000 * 60;
                      var hour = minute * 60;
                      var day = hour * 24;
                      var halfamonth = day * 15;
                      var month = day * 30;
                      var now = new Date().getTime(); //获取当前时间戳
                      var finvalue = now - years;  //获取时间差
                      if (finvalue < 0) {
                          return;
                      }
                      var monthnow = diffValue / month;
                      var weeknow = diffValue / (7 * day);
                      var daynow = diffValue / day;
                      var hournow = diffValue / hour;
                      var minnow = diffValue / minute;
                      if (monthnow >= 1) {
                          if (monthnow <= 12)
                              result = "" + parseInt(monthnow) + "月前";
                          else {
                              result = "" + parseInt(monthnow / 12) + "年前";
                          }
                      }
                      else if (weeknow >= 1) {
                          result = "" + parseInt(weeknow) + "周前";
                      }
                      else if (daynow >= 1) {
                          result = "" + parseInt(daynow) + "天前";
                      }
                      else if (hournow >= 1) {
                          result = "" + parseInt(hournow) + "小时前";
                      }
                      else if (minnow >= 1) {
                          result = "" + parseInt(minnow) + "分钟前";
                      } else {
                          result = "刚刚";
                      }

                      return result;
                  };
                  var addtime = getDateAfter(time)  //调用此方法转换time时间
                  //console.log(addtime)   //某天前

你可能感兴趣的:(小程序)