获取一组时间中的最近的日期

        function getLatelyDate(arrDate) {
            var arrMs = arrDate.map(function(time) {
                return new Date(time.replace(/\-/g, "\/")).getTime();
            }, this);
            var max = Math.max.apply(null, arrMs);
            var date = new Date(max);
            return `${date.getFullYear()}-${this.turn(date.getMonth()+1)}-${this.turn(date.getDate())}`;
        };
        function turn(str) {
            str = String(str);
            return str.length === 1 ? '0'+str : str;
        };


        var time = getLatelyDate(['2018-06-27', '2017-06-21', '2015-02-2', '2017-08-2', '2017-04-15']);
        console.log(time);    // 2018-06-27

 

posted @ 2017-06-30 13:59  Sorrow.X  阅读(328)  评论(0编辑  收藏  举报