codecamp

日期改写算法挑战

function makeFriendlyDates(arr) {
        var fArr = arr[0].split('-'),
            tArr = arr[1].split('-'),
            fY = fArr[0],
            fM = fArr[1],
            fD = fArr[2],
            tY = tArr[0],
            tM = tArr[1],
            tD = tArr[2],
            mArr = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            dArr = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th'], 
            today = new Date(),
            rArr=[];
        if (arr[0] === arr[1]) {
            rArr[0] = mArr[parseInt(fM) - 1] + ' ' + dArr[parseInt(fD) - 1] + ', ' + fY;
            return rArr;
        }
        if ((new Date(parseInt(tY), parseInt(tM) - 1, parseInt(tD))).getTime() - (new Date(parseInt(fY), parseInt(fM) - 1, parseInt(fD))).getTime() < 3600000 * 24 * 365)
        {
            if (parseInt(fY) === 2016) {
                rArr[0] = mArr[parseInt(fM) - 1] + ' ' + dArr[parseInt(fD) - 1];
            } else {
                rArr[0] = mArr[parseInt(fM) - 1] + ' ' + dArr[parseInt(fD) - 1] + ', ' + fY;
            }
            if (tM === fM && tY===fY) {
                rArr[1] = dArr[parseInt(tD) - 1];
            } else {
                rArr[1] = mArr[parseInt(tM) - 1] + ' ' + dArr[parseInt(tD) - 1];
            }
        } else {
            rArr[0] = mArr[parseInt(fM) - 1] + ' ' + dArr[parseInt(fD) - 1] + ', ' + fY;
            rArr[1] = mArr[parseInt(tM) - 1] + ' ' + dArr[parseInt(tD) - 1] + ', ' + tY;
        }
        return rArr;
    }


makeFriendlyDates(["2016-07-01", "2016-07-04"]);
排列组合去重算法挑战
类及对象构建算法挑战
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定