IOS跟Chrome和android上new Date()变现不一致

1,162 阅读2分钟

今天在处理关于时间的需求问题上时,发现了一个问题,在ios跟chrome和安卓上new Date()的结果表现不一致

ios 上创建new Date只支持new Date('YYYY/MM/DD')这种写法不然不会转换成功

而安卓chrome支持 new Date('YYYY-MM-DD')跟new Date('YYYY/MM/DD') 两种写法

出现这个问题的原因是内核问题,可以为什么会这样我也不清楚,搜了半天资料也没搜到,希望有大佬留言解惑。。

而且还有一点是日期超了ios上面是把时间往后加变成了新月份而chrome是直接报错。所以没办法了老老实实年月日挨个处理判断吧

// 判断是否是正常日期
    isNormalDate(year, month, day) {
        const checkYear = parseInt(year, 10);
        const checkMonth = parseInt(month, 10);
        const checkDay = parseInt(day, 10);
        let isNormal = true;
        if (checkMonth > 12 || checkMonth < 1) {
            isNormal = false;
        } else {
            let isMonthDays = null;
            if (this.isLeapYear(checkYear)) {
                isMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            } else {
                isMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            }
            if (isMonthDays[checkMonth - 1] < checkDay || checkDay <= 0) {
                isNormal = false;
            }
        }
        return isNormal;
    },
    // 判断闰年
    isLeapYear(year) {
        if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
            return true;
        } else {
            return false;
        }
    },
    
    
     // 判别星座
    constellation(month, day) {
        const currentMonth = parseInt(month, 10);
        const currentDay = parseInt(day, 10);
        let currentConstellation = '';
        if ((currentMonth === 1 && currentDay > 21) || (currentMonth === 2 && currentDay <= 19)) {
            currentConstellation = Constellation.aquarius;
        } else if ((currentMonth === 2 && currentDay >= 20) || (currentMonth === 3 && currentDay <= 20)) {
            currentConstellation = Constellation.pisces;
        } else if ((currentMonth === 3 && currentDay >= 21) || (currentMonth === 4 && currentDay <= 20)) {
            currentConstellation = Constellation.aries;
        } else if ((currentMonth === 4 && currentDay >= 21) || (currentMonth === 5 && currentDay <= 21)) {
            currentConstellation = Constellation.taurus;
        } else if ((currentMonth === 5 && currentDay >= 22) || (currentMonth === 6 && currentDay <= 21)) {
            currentConstellation = Constellation.gemini;
        } else if ((currentMonth === 6 && currentDay >= 22) || (currentMonth === 7 && currentDay <= 23)) {
            currentConstellation = Constellation.cancer;
        } else if ((currentMonth === 7 && currentDay >= 24) || (currentMonth === 8 && currentDay <= 23)) {
            currentConstellation = Constellation.leo;
        } else if ((currentMonth === 8 && currentDay >= 24) || (currentMonth === 9 && currentDay <= 23)) {
            currentConstellation = Constellation.virgo;
        } else if ((currentMonth === 9 && currentDay >= 24) || (currentMonth === 10 && currentDay <= 23)) {
            currentConstellation = Constellation.libra;
        } else if ((currentMonth === 10 && currentDay >= 24) || (currentMonth === 11 && currentDay <= 22)) {
            currentConstellation = Constellation.scorpio;
        } else if ((currentMonth === 11 && currentDay >= 23) || (currentMonth === 12 && currentDay <= 22)) {
            currentConstellation = Constellation.sagittarius;
        } else if ((currentMonth === 12 && currentDay >= 23) || (currentMonth === 1 && currentDay <= 21)) {
            currentConstellation = Constellation.capricorn;
        }
        return currentConstellation;
    },