日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

小程序獲取今天,昨天,前天,上月,上周時間全

作者:Zan^Z 更新時間: 2022-02-20 編程語言

page 因為我需要向接口里傳遞時間參數(shù),所以我在onLoad里獲取

var that=this;
//今天
var time = util.formatData(new Date());

//昨天
that.getDateStr(null, -1);     //獲取昨天的日期

that.getDateStrTwo(null, -2);     //獲取前天的日期

that.getTimeLastWeek(7), //上周一的日期
that.getTimeLastWeek(1)  //上周日的日期(一個星期的開始和結(jié)束日期)

自定義函數(shù)
今天
就只有“今天”寫入在util.js里,只獲取年月日,格式為2020-03-11,module.exports需要調(diào)用

const formatData = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}
module.exports = {
  formatData: formatData,
}

昨天和上月1號到上月本日

getDateStr: function (today, addDayCount) {
    var date;
    if (today) {
      date = new Date(today);
    } else {
      date = new Date();
    }
    date.setDate(date.getDate() + addDayCount);//獲取AddDayCount天后的日期 
    var y = date.getFullYear();
    var m = date.getMonth() + 1;//獲取當(dāng)前月份的日期 
    var d = date.getDate();

    var up_m = date.getMonth(); //獲取上月
    var up_d = 1;               //獲取上月1號
    var up_br = date.getDate() + 1; //上月本日
    if (m < up_m) {
      y = y - 1
    }
    if (m < 10) { 
      m = '0' + m; 
    };
    if (up_m < 10) {
      up_m = '0' + up_m;
    };
    if (d < 10) {
      d = '0' + d;
    };
    if (up_d < 10) {
      up_d = '0' + up_d;
    };
    if (up_br < 10) {
      up_br = '0' + up_br;
    };
    
    this.setData({
      yestodayStartTime: y + "-" + m + "-" + d,
      yestodayEndTime:y + "-" + m + "-" + d,
      up_monthStartTime: y + "-" + up_m + "-" + up_d,
      up_monthEndTime: y + "-" + up_m + "-" + up_br,
    })
    //console.log(y + "-" + m + "-" + d)
    return y + "-" + m + "-" + d;
  },

前天

getDateStrTwo: function (today, addDayCount) {
    var date;
    if (today) {
      date = new Date(today);
    } else {
      date = new Date();
    }
    date.setDate(date.getDate() + addDayCount);//獲取AddDayCount天后的日期 
    var y = date.getFullYear();
    var m = date.getMonth() + 1;//獲取當(dāng)前月份的日期 
    var d = date.getDate();
    if (m < 10) {
      m = '0' + m;
    };
    if (d < 10) {
      d = '0' + d;
    };
    this.setData({
      twoBeforStartTime: y + "-" + m + "-" + d,
      twoBeforEndTime: y + "-" + m + "-" + d,
    })
    //console.log(this.data.twoBeforStartTime);
    return y + "-" + m + "-" + d;
  },

上周(從周一到周日的日期)

getTimeLastWeek:function(n){
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDay(); //返回星期幾的某一天;
    n = day == 0 ? n + 6 : n + (day - 1)
    now.setDate(now.getDate() - n);
    var date = now.getDate();
    var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
    return s;
  }
})

原文鏈接:https://blog.csdn.net/Zan_Z/article/details/104800099

欄目分類
最近更新