后端传过来的数值是时间,需要判断一下这个时间与当前时间的前后,也就是有没有超时,
方法一:调整后端传过来的时间数据,与用当前时间函数获取的时间进行对比(这里还没有写完)
//判断是否超时
timeChecked(val){
if(val != null){
let arrTime = val.split(" ")
let date = arrTime[0].split("-")
let time = arrTime[1].split(":")
let formatDate=new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2])
let CURRENTTIME = this.nowTime()
if(val>CURRENTTIME){
console.log("未超时")
return 0;
}else{
console.log("超时")
return 1;
}
}
},
//获取当前时间
nowTime() {
let now = new Date()
},
方法二:调整用当前函数获取的时间的形式,与后端传过来的时间数据进行对比。(亲测有效)
//判断是否超时
timeChecked(val){
if(val != null){
let CURRENTTIME = this.nowTime()
if(val>CURRENTTIME){
console.log("未超时")
return 0;
}else{
console.log("超时")
return 1;
}
}
},
//获取当前时间
nowTime() {
let now = new Date()
let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
let _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
let _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
let _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
return now.getFullYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
},
两种方法殊途同归,最后都能实现判断是否超时的目的。
var time = new Date();
time.getYear(); //获取当前年份
time.getFullYear(); //获取完整的年份(4位,1970-???)
time.getMonth(); //获取当前月份(0-11,0代表1月)
time.getDate(); //获取当前日(1-31)
time.getDay(); //获取当前星期X(0-6,0代表星期天)
time.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
time.getHours(); //获取当前小时数(0-23)
time.getMinutes(); //获取当前分钟数(0-59)
time.getSeconds(); //获取当前秒数(0-59)
time.getMilliseconds(); //获取当前毫秒数(0-999)
time.toLocaleDateString(); //获取当前日期
var mytime=time.toLocaleTimeString(); //获取当前时间
time.toLocaleString( ); //获取日期与时间
可以打印出来试一下。