当前位置: 首页 > news >正文

关于时间向前推算到天,并且算闰年的计算

直接上代码了,具体需求看项目要求
body里面的代码是这样的,用的是elementUI
这里是设计要求,但是你要是往前推就可以用这个,当然关于当前是日期是否禁选,你直接参考elementUI就好了

在这里插入图片描述

        <el-row class="mt24">
          <el-col :span="12">
            <el-form-item
              label="周期单位"
              prop="periodUnit"
            >
              <el-select
                v-model="ruleForm.periodUnit"
                :placeholder="$t('Cyc.Pleaseselect')"
                :disabled="action == 3 ? true : false"
                class="w240"
                @change="_dateChange('periodUnit', 1)"
              >
                <el-option
                  v-for="item in unitsList"
                  :key="item.id"
                  :label="item.dictName"
                  :value="item.dictCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!--周期:  -->
          <el-col :span="12">
            <el-form-item
              label="-周期:"
              prop="periodNum"
            >
              <el-input
                v-model.number="ruleForm.periodNum"
                autocomplete="off"
                class="w240"
                :placeholder="$t('Cyc.Pleaseenter')"
                maxlength="32"
                :disabled="action == 3 ? true : false"
                @blur="_period"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
          <!-- 上次巡视时间: -->
          <el-col :span="12">
            <el-form-item
              label="上次巡视时间"
              prop="prePatrolDate"
            >
              <el-date-picker
                v-model="ruleForm.prePatrolDate"
                align="right"
                autocomplete="off"
                class="w240"
                :placeholder="$t('Cyc.PleaseselectprePatrolDate')"
                maxlength="32"
                style="width: 240px"
                format="yyyy-MM-dd"
                @change="getTimeaaa"
                value-format="yyyy-MM-dd"
                :type="timeType"
                :disabled="action == 3 || iszhouqi ? true : false"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
       我们页面的要求是周期单位(月,日)跟上次巡视时间date有关系,
       时间跟周期有关系。

js中

  <script>
	function getNewDate(many) {
	  let thirtyDays = [4, 6, 9, 11]; //30天的月份
	  let thirtyOneDays = [1, 3, 5, 7, 8, 10, 12]; //31天的月份
	  let currDate = new Date(); //今天日期
	  let dateMilli = 0; //时间戳
	  let year = currDate.getFullYear(); // 当前年
	  let month = currDate.getMonth() + 1; // 当前月
	  let targetDateMilli = 0; //目标日期时间戳
	  // let targetDays = ""; //目标日期
	  // let targetYear = ""; //目标年
	  // let targetMonth = ""; //目标月
	  // let targetDate = ""; //目标时间
	  // let dealTargetDays = ""; //处理格式目标日期
	
	  let isLeapYear =
	    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? true : false; //是否是闰年
	  let countDays = 0; //累计天数
	  for (let i = 0; i < many; i++) {
	    month--;
	    thirtyDays.includes(month)
	      ? (countDays += 30)
	      : thirtyOneDays.includes(month)
	      ? (countDays += 31)
	      : isLeapYear
	      ? (countDays += 29)
	      : (countDays += 28);
	  }
	  dateMilli = currDate.getTime(); // //时间戳
	  targetDateMilli = dateMilli - countDays * 8.64e7;
	  return targetDateMilli;
	}
	
	export default {
	  name: "zqModel",
	  computed: {
	    pickerOptions() {
	      const _this = this;
	      return {
	        disabledDate(time) {
	          let nowDate = new Date().getTime(); //当前日期
	          if (_this.ruleForm.periodUnit == "MONTH") {
	            return (
	              time.getTime() > Date.now() - 24 * 60 * 60 * 1000 ||
	              time.getTime() <
	                getNewDate(_this.ruleForm.periodNum) - 24 * 60 * 60 * 1000
	            );
	          } else {
	            //天
	            let cycle = _this.ruleForm.periodNum * 24 * 3600 * 1000;
	            let diff = nowDate - cycle; //差值
	            return (
	              time.getTime() > Date.now() - 24 * 60 * 60 * 1000 ||
	              time.getTime() + 24 * 60 * 60 * 1000 < diff
	            );
	          }
	        }
	      };
	    }
	  },
	  watch: {
	    "ruleForm.periodNum"(periodNum) {
	      //如果状态action 1新增2修改3查看
	      if (this.action != "3") {
	        if (!this.isinit) {
	          //如果不是初始化的时候 日期为空
	          this.ruleForm.prePatrolDate = "";
	        }
	        this.isinit = false;
	        if (this.ruleForm.periodUnit == "" || periodNum == "") {
	          this.timeType = "";
	        } else {
	          this.timeType = "date";
	        }
	      }
	    },
	    action(val) {
	      this.ruleForm = {
	        periodUnit: "", //周期单位
	        periodNum: "", //--周期
	        prePatrolDate: "", // 上次巡视时间 应该是返回回来的
	      };
	    },
	  },
	
	  data() {
	    return {
	      ruleForm: {
	        periodUnit: "", //周期单位
	        periodNum: "", //--周期
	        prePatrolDate: "", // 上次巡视时间 应该是返回回来的
	      },
	      rules: {
	        periodUnit: [
	          {
	            required: true,
	            message: this.$t("phrase.Cannotempty"),
	            trigger: "blur"
	          }
	        ],
	        periodNum: [
	          {
	            required: true,
	            validator: validateFloat,
	            trigger: "blur"
	          }
	        ],
	
	        prePatrolDate: [
	          {
	            required: true,
	            message: this.$t("phrase.Cannotempty"),
	            trigger: "blur"
	          }
	        ]
	      },
	      firstlist: [], //巡视类型
	      unitsList: [], //周期单位
	      timeType: "date", // 时间类型
	    };
	  },
	  created() {
	    console.log("ddddd");
	    this._getDates(); //获取字典表
	  },
	  methods: {
	    //巡视类型 返回的名字
	    _returnNames(id) {
	      let name;
	      this.firstlist.forEach(item => {
	        if (item.dictCode == id) {
	          name = item.dictName;
	        }
	      });
	      return name;
	    },
	    
	    timeTypeFun(data) {
	      let date = new Date(data);
	      let Y = date.getFullYear();
	      let M = date.getMonth() + 1;
	      if (M < 10) {
	        M = "0" + M;
	      }
	      let D = date.getDate();
	      if (D < 10) {
	        D = "0" + D;
	      }
	      return `${Y}-${M}-${D}`;
	    },
	
	    // 周期改变
	    _period(val) {
	      console.log(val);
	      this.$refs["ruleForm"].clearValidate(val);
	    },
	    //点击 周期单位改变
	    _dateChange(newvalue, i) {
	      this.$refs["ruleForm"].clearValidate(newvalue);
	      if (i == 1) {
	        this.ruleForm.prePatrolDate = "";
	      }
	
	      if (this.ruleForm.periodNum != "") {
	        this.timeType = "date";
	      } else {
	        this.timeType = "";
	      }
	    },
	    _datewenen(val, e) {
	      this.$refs["ruleForm"].clearValidate(val);
	    },
	    // 字典获取
	      //   // 周期单位 获取日月  时间类型
	      dictData({ root: "T_DICT_TIMETYPE" }).then(res => {
	        if (res.code == "SUCCESS") {
	          this.unitsList.push(res.data[1]); //月
	          this.unitsList.push(res.data[2]); //日
	        }
	      });
	    },
	
	    getTimeaaa() {
	      this.ruleForm = Object.assign({}, this.ruleForm);
	    },
	    // 获取详情
	    _getInfo(id) {
	      getlistInfo({ id: id }).then(res => {
	        if (res.code == "SUCCESS") {
	          console.log(res, "获取详情信息");
	          this.ruleForm.patrolType = res.data.patrolType; //类型
	          this.ruleForm.orgName = res.data.orgName; //
	          this.ruleForm.periodUnit = res.data.periodUnit; //单位
	          this.ruleForm.periodNum = res.data.periodNum; // 周期
	          this.ruleForm.prePatrolDate = res.data.prePatrolDate; //时间
	          this.ruleForm.workTextId = res.data.workTextId; //文本id 名称
	          this.ruleForm.patrolDeviceId = res.data.patrolDeviceId; //巡视设备id
	          this.ruleForm.userId = res.data.userId; //人员id
	          this.ruleForm.id = res.data.id;
	          this.chargeLists = res.data.userRealName; //负责人数据
	          this.chargeListSb = res.data.deviceName; //设备
	          this.ruleForm = Object.assign({}, this.ruleForm);
	        }
	
	        if (this.ruleForm.periodUnit != "" || this.ruleForm.periodNum != "") {
	          this.isinit = true;
	        }
	      });
	    },
	    // 取消
	    // 提交
	    submitForm(ruleForm) {
	    },
	    // 新增
	    _add() {
	      );
	};
</script>

相关文章:

  • Android传递Bitmap的两种简单方式及其缺陷
  • Android中的mvp
  • 前端初始化项目对axios的封装和token的存储应用以及config.js代理的配置 比较全的了。
  • 开机自启动redis
  • 在一个div标签中平行放置两个echarts 环形图
  • node-webkit,html打包成桌面应用,pc应用
  • 【HTML5】Web存储
  • 利用 vuex写一个todoList
  • Java日志组件2---Log4j(org.apache.log4j.Logger)
  • react项目搭建
  • mybatis3.0 配置等值连接两种方式:resultMap和resulttype
  • react官方脚手架安装
  • 怎么用ChemDraw 15.1 Pro绘制彩色结构
  • react实现打印功能
  • 关于CreateProcess函数一些经验
  • 《Java编程思想》读书笔记-对象导论
  • Apache的基本使用
  • cookie和session
  • co模块的前端实现
  • Hibernate【inverse和cascade属性】知识要点
  • Idea+maven+scala构建包并在spark on yarn 运行
  • Java应用性能调优
  • JS+CSS实现数字滚动
  • JS变量作用域
  • JS实现简单的MVC模式开发小游戏
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • MySQL QA
  • Octave 入门
  • Rancher如何对接Ceph-RBD块存储
  • scrapy学习之路4(itemloder的使用)
  • supervisor 永不挂掉的进程 安装以及使用
  • tweak 支持第三方库
  • TypeScript迭代器
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 关于 Cirru Editor 存储格式
  • 码农张的Bug人生 - 初来乍到
  • 面试总结JavaScript篇
  • 前端之React实战:创建跨平台的项目架构
  • 三栏布局总结
  • 我与Jetbrains的这些年
  • 因为阿里,他们成了“杭漂”
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​你们这样子,耽误我的工作进度怎么办?
  • (4)(4.6) Triducer
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (二)构建dubbo分布式平台-平台功能导图
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (一)u-boot-nand.bin的下载
  • (转) Face-Resources
  • (转)Mysql的优化设置
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作