首页 > 技术文章 > js日期选择器日期(选择框前进一天、后退一天)

lxc127136 2020-07-20 17:13 原文


1. vue 框架
<el-col :span="8" style="margin-left:3px;">
   <el-button type="danger" size="mini" @click="onday">上一日</el-button> 
   <el-button type="primary" size="mini" @click="nextday()">下一日</el-button>
 </el-col>

2. 日期不够0 自动补0

 //搜索上一日数据
    onday(){
      let today = new Date(this.searchVal.dt);
      let targetday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24;
      today.setTime(targetday_milliseconds); // 注意,这行是关键代码
      let CurrentDate = "";
      let tYear = today.getFullYear();
      let tMonth = today.getMonth();
      let tDate = today.getDate();
      tMonth = tMonth + 1;
      CurrentDate += tYear + "-";
      if (tMonth >= 10 ){
        CurrentDate += tMonth + "-";
      }else{
        CurrentDate += "0" + tMonth + "-";
      }
      if (tDate >= 10 ){
        CurrentDate += tDate ;
      }else{
        CurrentDate += "0" + tDate ;
      }
      console.log(CurrentDate)
    },
    //搜索下一日数据
    nextday(){
      let today = new Date(this.searchVal.dt);
      let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24;
      today.setTime(targetday_milliseconds); // 注意,这行是关键代码
      let CurrentDate = "";
      let tYear = today.getFullYear();
      let tMonth = today.getMonth();
      let tDate = today.getDate();
      tMonth = tMonth + 1;
      CurrentDate += tYear + "-";
      if (tMonth >= 10 ){
        CurrentDate += tMonth + "-";
      }else{
        CurrentDate += "0" + tMonth + "-";
      }
      if (tDate >= 10 ){
        CurrentDate += tDate ;
      }else{
        CurrentDate += "0" + tDate ;
      }
      console.log(CurrentDate)
    },

3.效果;

 

 点击上一日和下一日:

 

 

希望对大家有所帮助;

推荐阅读