一.new Date(时间) IOS兼容的问题
当时间格式为yyyy-MM-dd HH:mm:ss时,即日期使用-连接时,ios会无法识别,返回invalid Date无法识别和转换
解决方法:
1. 使用replace替换,直接将 - 替换为 / 这样安卓和IOS就都可以识别了
new Date("yyyy-MM-dd HH:mm:ss").replace(/-/g, "/");
2.使用moment.js
import moment from 'moment';
//使用moment设置时间
moment('yyyy-MM-dd HH:mm:ss');
3. IOS的new Date()其实是有格式要求的,就是需要使用/来做分割符,
二.判断所选择的日期是否在六个内
1.第一种方法
let dt1 = val.timeSelection[0] + " 00:00:00";
let dt2 = val.timeSelection[1] + " 00:00:00";
let date1 = new Date(dt1.replace(/-/g, "/"));
let date2 = new Date(dt2.replace(/-/g, "/"));
let curVal =
(Date.parse(date2) - Date.parse(date1)) / 1000 / 60 / 60 / 24;
if (curVal < 0) {
curVal = curVal * -1;
}
if (curVal > 180) {
console.log("时间范围选择6个月以内");
return false;
}
2..第二种方法
const dt1 = val.timeSelection[0] + " 00:00:00";
const dt2 = val.timeSelection[1] + " 00:00:00";
const monthDiff = Math.abs(
(new Date(dt2).getFullYear() - new Date(dt1).getFullYear()) * 12 +
(new Date(dt2).getMonth() - new Date(dt1).getMonth())
);
console.log(monthDiff);
if (monthDiff > 6) {
console.log("时间范围选择6个月以内");
return false;
}