获取时间差 (当前系统时间 - 后台返回的时间)
//获取时间差 (当前系统时间)2019-09-27 18:42:47 减去 2019-09-27 16:42:47function getTime(time){ var times = time_year='',time_month='',time_day='',time_hour=''; var date = new Date(); var year = date.getFullYear(); var month = date.getMonth()+1; var day = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); //console.log(year+'-'+month+'-'+day+' -'+hour+':'+minute+':'+second); times = time.split('-'); time_year = times[0];//年 time_month = times[1];//月 time_day = times[2].split(' ')[0];//日 time_hour = times[2].split(' ')[1]; time_hour = time_hour.split(':')[0];//时 time_year = year - time_year time_month = month - time_month; time_day = day - time_day; time_hour = hour - time_hour; if(time_year >0){ return time_year+'年'+time_month+'月'+time_day+'天'+time_hour+'小时' }else{ if(time_month >0){ return time_month+'月'+time_day+'天'+time_hour+'小时' }else{ if(time_day >0){ return time_day+'天'+time_hour+'小时' }else{ return time_hour+'小时前' } } }}
日期拼接格式化为 9月12号 星期四 09:10形式
//日期拼接格式化为 9月12号 星期四 09:10形式//month:月份 data:几号 day:数字1-7function matchDatas(month,date,day){ var datas ='',re_month='01',re_data = '30',re_day="1"; var re_month= month +1; var re_data= date; var re_day= day; if(re_day == '1'){ re_day ='星期一'; }else if(re_day == '2'){ re_day ='星期二'; }else if(re_day == '3'){ re_day ='星期三'; }else if(re_day == '4'){ re_day ='星期四'; }else if(re_day == '5'){ re_day ='星期五'; }else if(re_day == '6'){ re_day ='星期六'; }else{ re_day ='星期日'; } datas = re_month + '月' + re_data +'号 '+ re_day; return datas}
时间拼接如:2019-10-09 17:24:11.0与 19:00换成 2019-10-09 19:00再加45分钟
//时间拼接2019-10-09 17:24:11.0与 19:00换成 2019-10-09 19:00再加45分钟function cutOffTime(creattime,starttime){ var datas='',clock='',minute='' if(starttime.indexOf('-') == -1){ datas = creattime.split(' ')[0];//得到2019-10-09 minute = +starttime.split(':')[1] + 45; //分钟先转成数字,再加45 if(minute >60){ //如果大于60分钟,时钟+1 minute = minute - 60; clock = +starttime.split(':')[0] + 1; }else{ clock = +starttime.split(':')[0]; } return datas+' '+clock+':'+minute //return clock+':'+minute }else{ datas= starttime.split(' ')[0]; //得到2019-10-09 var clock_minute = starttime.split(' ')[1]; //15:32:25 minute = +clock_minute.split(':')[1] + 45;//分钟先转成数字,再加45 if(minute >60){ //如果大于60分钟,时钟+1 minute = minute - 60; clock = +clock_minute.split(':')[0] + 1; }else{ clock = +starttime.split(':')[0]; } return datas+' '+clock+':'+minute }}
毫秒转时分秒1
//毫秒转时分秒function changeTime(time){ var days = time / 1000 / 60 / 60 / 24; var daysRound = Math.floor(days); var hours = time/ 1000 / 60 / 60 - (24 * daysRound); var hoursRound = Math.floor(hours); var minutes = time / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); var minutesRound = Math.floor(minutes); var seconds = time/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); if(daysRound == 0){ if(hoursRound == 0){ return '刚刚' } return hoursRound +'小时前' } return daysRound +'天'+ hoursRound +'小时前'}
毫秒转时分秒2
var times = 1566354720000changeTime(times);//毫秒转时分秒function changeTime(time){ var days = time / 1000 / 60 / 60 / 24; var daysRound = Math.floor(days); var hours = time/ 1000 / 60 / 60 - (24 * daysRound); var hoursRound = Math.floor(hours); var minutes = time / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); var minutesRound = Math.floor(minutes); var seconds = time/ 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); if(daysRound == 0){ if(hoursRound == 0){ if(minutesRound == 0){ return seconds +'秒' } return minutesRound +'分'+ seconds +'秒' } return hoursRound +'小时'+ minutesRound +'分'+ seconds +'秒' } return daysRound +'天'+ hoursRound +'小时'+ minutesRound +'分'+ seconds +'秒'}
时间比较(这种写法不太好,更优的做法是转成时间戳再比较new Date().getTime())
//时间比较 //排除了两个时间是同一天的 comparisonTime(createTime, nowTime, message) { if (nowTime != '') { let createDate = createTime.toString() var stringDate = new Date(createDate); var arrDate = [stringDate.getFullYear(), stringDate.getMonth() + 1, stringDate.getDate()]; for (var i = 0, len = arrDate.length; i < len; i++) { if (arrDate[i] < 10) { arrDate[i] = '0' + arrDate[i]; } } let purcharr = nowTime.split('-'); if (purcharr[0] >= arrDate[0]) { //年 if (purcharr[1] >= arrDate[1]) { //月 if (purcharr[2] >= arrDate[2]) { //日 return true; } else { this.$message({ showClose: true, message: message, type: 'error' }); return false; } } else { this.$message({ showClose: true, message: message, type: 'error' }); return false; } } else { this.$message({ showClose: true, message: message, type: 'error' }); return false; } } },
校验时间(座机和手机号都会校验)
checkTel(val, dataType) { var mobilevalid = /^((0\d{2,3}-\d{7,8})|(1[3|4|5|7|8]\d{9}))$/; if (!mobilevalid.test(val)) { this.$notify({ type: MsgType.INFO, message: '请输入正确的联系电话!' }) return false; } },