小程序时必用的各种处理时间函数倒计时等
发布于 4 年前 作者 gyu 1030 次浏览 来自 分享
calcTime(str,seeTime) {

    let date1 = new Date(str);  //开始时间

    let date2 = new Date();  //结束时间

    let date3 = date2.getTime() - date1.getTime()  //时间差的毫秒数

    let isFirst = (seeTime - date2.getTime() > 0) ? true:false;

    let isOvertime = false;

 

    //计算出相差天数

    let days = Math.floor(date3 / (24 * 3600 * 1000))

    //计算出小时数

    let leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数

    let hours = Math.floor(leave1 / (3600 * 1000))

    //计算相差分钟数

    let leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数

    let minutes = Math.floor(leave2 / (60 * 1000))

    //计算相差秒数

    let leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数

    let seconds = Math.round(leave3 / 1000);

 

    if (hours <= 0) {

      hours = 0;

    } else {

      isOvertime = true;

    }

    if (minutes <= 0) {

      minutes = '00';

    } else if (minutes > 3) {

      isOvertime = true;

    }

    if (minutes.toString().length == 1) {

      minutes = '0' + minutes;

    }

    if (seconds < 0) {

      seconds = '00';

    }

    if (seconds.toString().length == 1) {

      seconds = '0' + seconds;

    }

    return [hours + ":" + minutes + ":" + seconds, isOvertime,isFirst];

  },

2.处理时间戳转化为各种想要的格式日期

function time(time, type) {

  var datetime = new Date();

  datetime.setTime(time);

  var year = datetime.getFullYear();

  var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;

  var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();

  var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();

  var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();

  var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();

  if (type == '1') {

      return year + "." + month + "-" + date + "日";

  }

  if (type == '2') {

      return year + "-" + month + "-" + date;

  }

  if (type == '3') {

      return month + "-" + date;

  }

  if(type == '4'){

      return month;

  }

  if(type == '5'){

      return date;

  }

  if(type == '6'){

      return year + "." + month + "." + date + " " + hour + ":" + minute + ":" + second;

  }

  if(type == '7'){

      return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;

  }

  if(type == '8'){

      return year + "-" + month + "-" + date + " " + hour + ":" + minute;

  }

  if(type == '9'){

      return month + "-" + date + " " + hour + ":" + minute;

  }

  if(type == '10'){

    return  hour + ":" + minute

}

}

3.获取当前时间日期

function getNowTime() {

  var now = new Date();

  var year = now.getFullYear();

  var month = now.getMonth() + 1;

  var day = now.getDate();

  if (month < 10) {

    month = '0' + month;

  };

  if (day < 10) {

    day = '0' + day;

  };

  //  如果需要时分秒,就放开

  // var h = now.getHours();

  // var m = now.getMinutes();

  // var s = now.getSeconds();

  var formatDate = year + '-' + month + '-' + day;

  return formatDate;

}

4.获取前一天的日期

function getDay(num, str) {

  var today = new Date();

  var nowTime = today.getTime();

  var ms = 24*3600*1000*num;

  today.setTime(parseInt(nowTime + ms));

  var oYear = today.getFullYear();

  var oMoth = (today.getMonth() + 1).toString();

  if (oMoth.length <= 1) oMoth = '0' + oMoth;

  var oDay = today.getDate().toString();

  if (oDay.length <= 1) oDay = '0' + oDay;

  return oYear + str + oMoth + str + oDay;

}
回到顶部