js中字符转日期转毫秒判断开始时间与结束时间的先后

来源:互联网 发布:问卷数据分析怎么写 编辑:程序博客网 时间:2024/05/21 10:41

在工作中我们时常会遇到根据开始时间和结束时间作为参数去查询数据,有时候需要对开始时间和结束时间进行比较,提示用户开始时间要小于结束时间或者开始时间与结束时间的可选取区间


var accessStart = "2017-1-2"var accessEnd = "2017-1-3"if ((accessStart != null && accessStart != "") && (accessEnd != null && accessEnd != "")) { // 字符转换为日期  accessStart = accessStart.replace(new RegExp(/-/g), '/');  var date = new Date(accessStart);      var year = date.getFullYear();  var month = date.getMonth() + 1;  if (month < 10) {  month = '0' + month;  }  var day = date.getDate();  if (day < 10) {  day = '0' + day;  }  accessEnd = accessEnd.replace(new RegExp(/-/g), '/');  var date_end = new Date(accessEnd);  var year = date_end.getFullYear();  var month = date_end.getMonth() + 1;  if (month < 10) {  month = '0' + month;  }  var day = date_end.getDate();  if (day < 10) {  day = '0' + day;  }  // 转换成毫秒比较两,结束时间与开始时间求差值      var timeseconds = new Date(accessEnd).getTime() - new Date(accessStart).getTime();  // 结束时间都是23:59:59      accessEnd = year + "-" + month + "-" + day + " 23:59:59";  // 开始时间都是00:00:00      accessStart = year + "-" + month + "-" + day + " 00:00:00";  if (timeseconds < 0) {Artery.showWarning("访问开始日期不能大于访问结束日期,请重新选择");return;  // 比较差值是否大于4天的毫秒值       } else if (timeseconds > 4 * 24 * 3600 * 1000) {Artery.showWarning("访问时间最多为5天,请重新选择");return;}


0 0
原创粉丝点击