温州高端网站建设公司,想设计一个公司的网站,手机排版软件app,软文推广营销js选中起始时间使用标准时间毫秒值计算一年后的当前少一天的日期
实际代码里面带入默认日期’20230301’这个特殊日期#xff0c;因为下一年的当前日期少一天为闰年的2月会有29天#xff0c;使用特殊值校验代码效果图 HTML部分代码
el-button clickchengTime()因为下一年的当前日期少一天为闰年的2月会有29天使用特殊值校验代码效果图 HTML部分代码
el-button clickchengTime() typewarning按钮一/el-button
el-button clickchengTimes() typeinfo按钮二/el-buttonJS部分代码 // 将毫秒值时间转化为标准时间格式方法dateFormat(val){let datenew Date(val);let ydate.getFullYear();let mdate.getMonth()1;mm10?0m:m;let ddate.getDate();dd10?0d:d;let timey-m-d;return time},// 第一种方法获取开始时间计算一年后的当前时间少一天的日期chengTime(){let timeList[]let nowtime20230301 // 这里专门挑选了闰年2月最后一天的特殊数据进行测试let nowyearNumber(nowtime.substring(0,4))1let nowmonthnowtime.substring(4,6)let nowdaynowtime.substring(6,8)if(nowmonth02nowday29){nowmonth03;nowday01}timeList.push((nowyear));timeList.push(nowmonth);timeList.push(nowday);let timeStrtimeList[0]-timeList[1]-timeList[2]let oldtimenew Date(timeStr).getTime()-3600*24*1000let formattedTimethis.dateFormat(oldtime).replace(-, ).replace(-, )this.$message({message: 按钮一的值,${formattedTime},type: success})},// 第二种方法获取开始时间计算一年后的当前时间少一天的日期chengTimes(){let nowtime20230301 // 这里专门挑选了闰年2月最后一天的特殊数据进行测试let nowyearNumber(nowtime.substring(0,4))1; // 年份上加上1获取下一年的年数据let nowmonthparseInt(nowtime.substring(4,6))-1; // 根据new Date()获取时间数据的方法传入的月份需要减去1let nowdayparseInt(nowtime.substring(6,8))-1;// 天数上减去1获取少一天的数据let formattedTimethis.dateFormat(new Date(nowyear,nowmonth,nowday)).replace(-, ).replace(-, )this.$message({message: 按钮二的值,${formattedTime},type: error})}需要注意的是方法一在实际使用中需要考虑浏览器对于原生JS的支持版本我在实际使用中发现低版本的好像不兼容。使用方法二的话就兼容性更好更推荐使用方法二去获取下一年当前日期少一天的日期数据。