小程序 : js获取今日日期、加日期、减日期; 判断今日 、判断昨日

获取今日,日期加、日期减

效果图:

在这里插入图片描述

选择日期大于今日时

在这里插入图片描述

.js代码

Page({
  /**
   * 页面的初始数据
   */
  data: {
    choseDate: '', // 选择的时间
    week:'', //星期几
    oneDayMillions: 24*3600*1000,  //一天的毫秒数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getTodayDate() //初始化获取今天日期
  },
  //获取今天日期-初始化日期
  getTodayDate(){
    var nowDate = new Date();
    var year = nowDate.getFullYear(), month = nowDate.getMonth() + 1, day = nowDate.getDate()
    this.setData({
      choseDate: `${year}-${month}-${day}`,
    })
    this.panDuanWeek() //判断星期几
  },
//选择时间选择框改变查询时间,查询
bindDateChange: function(e) {
      var changeDay = e.detail.value
      this.panduanData(changeDay) //判断日期
},

//减时间
subtractDay: function () {
  var selectday = this.data.choseDate.replace(/-/g,"/");//替换字符,变成标准格式;
  var dateTime = new Date(selectday).getTime(); //转换为时间戳
  var subDay = new Date(dateTime - this.data.oneDayMillions); // 当前 天数-1
  var year = subDay.getFullYear(), month = subDay.getMonth() + 1, day = subDay.getDate();
  var changeDay = `${year}-${month}-${day}`;
  //判断日期
  this.panduanData(changeDay)
},
//加时间
addDay: function () {
  var selectday = this.data.choseDate.replace(/-/g,"/");//替换字符,变成标准格式;
  var dateTime = new Date(selectday).getTime(); //转换为时间戳
  var addday = new Date(dateTime + this.data.oneDayMillions); // 当前 天数+1
  var year = addday.getFullYear(), month = addday.getMonth() + 1, day = addday.getDate()
  var changeDay = `${year}-${month}-${day}`
  // console.log('addday选择天数加一',changeDay)
  //判断日期
  this.panduanData(changeDay) 
},
 //判断日期
 panduanData(changeDay){
  //获取今日时间戳
  var nowDate = new Date();
  var year = nowDate.getFullYear(), month = nowDate.getMonth() + 1, day = nowDate.getDate();
  var nowDay =`${year}/${month}/${day}`;
  var today = new Date(Date.parse(nowDay)).getTime(); 
  //选择的日期
  var selectday = changeDay.replace(/-/g,"/");
  var selectdDate = new Date(Date.parse(selectday)).getTime();
  //判断选择日期与今日的大小
    if(selectdDate > today){
        wx.showToast({
          title: '只能查看今日及以前的记录',
          icon: 'none',
          duration: 3000 //提示展示3秒关闭
        })
        return
      }else{
        this.setData({
          choseDate: changeDay //选择日期赋值
        })
          this.panDuanWeek() //判断星期几
      }
  
 },
    //判断星期几
 panDuanWeek(){
  //替换字符,变成标准格式※※※注意,日期格式一定要为xxxx/xx/xx,别的格式Android 和iOS有兼容问题。
  var date = this.data.choseDate.replace(/-/g,"/");
  var weekArray = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  var weekday = weekArray[new Date(date).getDay()];  //此处必须是先new一个Date
  this.setData({
    week:weekday,
  })
 },
 

})

.wxml

 <!-- 日期 -->
  <view class="art_SignMain pad20r">
    <view bindtap="subtractDay" class="btn_width">
      <image class="jianTou-img"  src='{{uploadUrl}}/icon/return_black_left_icon.png'></image>
    </view>
	    <picker mode="date" value="{{date}}" start="2010-01-01" end="2030-01-01" bindchange="bindDateChange">
	      <view class="picker-Data">
	        <text class="mgr-10"> {{choseDate}}</text>
	        <text>{{week}}</text>
	      </view> 	
	    </picker>
     <view bindtap="addDay" wx:if="{{showNextBtn}}" class="btn_width">
       <image class="jianTou-img" src='{{uploadUrl}}/icon/return_black_right_icon.png'></image>
     </view> 
      <view  wx:else class="btn_width">
       <image class="jianTou-img" src='{{uploadUrl}}/icon/return_black_right_y_icon.png'></image>
      </view> 
  </view>

加减日期的另外一种方法(麻烦不实用版本)

//加时间
addDay: function () {
  var count = this.data.choseDate;
  var year = parseInt(count.split("-")[0]);
  var month = parseInt(count.split("-")[1]);
  var day = parseInt(count.split("-")[2]);
  // 日期加
  day += 1;
  //判断对应的月份,确定该月有多少天
if(month == 4 || month == 6 || month == 9 || month == 11 ){
  if (day > 30) {
    month += 1;
    day = 1;
  }
  this.getDateTime(day,month,year)
} else if(month == 2){
  // 判断闰年
  if ( (year%4==0&&year%100!=0) ||  year%400==0  ){
    if (day > 29) {
      month += 1;
          day = 1;
        }  
    }else{
      if (day > 28) {
        month += 1;
        day = 1;
      }
    }
    this.getDateTime(day,month,year)

   
}else {
  if (day > 31) {
    month += 1;
    day = 1;
    if (month > 12) {
      year += 1;
      month = 1
    }
  }
  this.getDateTime(day,month,year)
}
},
//减日期
subtractDay: function () {
  var count = this.data.choseDate;
  var year = parseInt(count.split("-")[0]);
  var month = parseInt(count.split("-")[1]);
  var day = parseInt(count.split("-")[2]);
  day -= 1;
  this.setData({
    rightSign: true,
  })
  // //判断对应的月份,确定该月有多少天
  if (day < 1) {
      month -= 1;   
      //判断对应的月份,确定该月有多少天
      if(month == 4 || month == 6 || month == 9 || month == 11 ){
          day = 30;
          this.getDateTime(day,month,year)
        }else if(month == 2){
      // 判断闰年
        if ( (year%4==0&&year%100!=0) ||  year%400==0  ){
                day = 29;
          }else{
              day = 28;
          }
         this.getDateTime(day,month,year)
      }else{ 
          day = 31;
          if (month < 1) {
                  year -= 1;
                  month = 12
                }
          this.getDateTime(day,month,year)
      }
 }else {
   this.getDateTime(day,month,year)
}
},
// 拼接时间
getDateTime(day,month,year){
  if (day < 10) {
    if (month < 10) {
      var changeDay = year + '-0' + month + '-0' + day
    }
    if (month >= 10) {
           var changeDay = year + '-' + month + '-0' + day
    }
  }
  if (day >= 10) {
    if (month < 10) {
          var changeDay = year + '-0' + month + '-' + day
    }
    if (month >= 10) {
         var changeDay =  year + '-' + month + '-' + day
    }
  }
   //判断日期
   this.panduanData(changeDay)
},

判断所选日期是否是今日

 //判断今日
 panduanToday(){
  var nowDate = new Date();
  var year = nowDate.getFullYear(), month = nowDate.getMonth() + 1, day = nowDate.getDate();
  var nowDay =`${year}/${month}/${day}`;
  var selectday = this.data.choseDate.replace(/-/g,"/");//替换字符,变成标准格式
  var time = new Date(selectday).getTime(); //选择时间的时间戳
  var todayTime = new Date(nowDay).getTime(); // 当天凌晨的时间
  var tomorrow = new Date(todayTime + 24 * 60 * 60 * 1000).getTime(); // 明天凌晨的时间
   if(time < tomorrow && todayTime <= time){
     //time < tomorrow && todayTime <= time 为true
     console.log('是今日',)
   }else{
   // console.log('非今日')
   }
 },

判断所选日期是否是昨日

 // 判断昨日
 panduanYesterday(){
   var nowDate = new Date();
  var year = nowDate.getFullYear(), month = nowDate.getMonth() + 1, day = nowDate.getDate();
  var nowDay =`${year}/${month}/${day}`;
  var todayTime = new Date(nowDay).getTime(); // 当天凌晨的时间
  var choseDateFormate = this.data.choseDate.replace(/-/g,"/");//替换字符,变成标准格式
  var time = new Date(choseDateFormate).getTime(); //选择的时间
  var yesterdayTime = new Date(todayTime - 24 * 60 * 60 * 1000).getTime(); // 昨天凌晨的时间
  //  console.log('判断是否为昨日',time < todayTime && yesterdayTime <= time)
   if(time < todayTime && yesterdayTime <= time){
   // time < todayTime && yesterdayTime <= time 为true
    console.log('是昨日')
   }else{
    //非昨日
   }
 },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值