js时间字符串转Date对象

    var DATE_REGEXP = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})([T\\s](\\d{2}):(\\d{2}):(\\d{2})(\\.(\\d{3}))?)?.*");
    function toDate(dateString){
        if(DATE_REGEXP.test(dateString)){
            var timestamp = dateString.replace(DATE_REGEXP, function($all,$year,$month,$day,$part1,$hour,$minute,$second,$part2,$milliscond){
                var date = new Date($year, $month,$day, $hour||"00", $minute||"00", $second||"00", $milliscond||"00");
                return date.getTime();
            });
            var date = new Date();
            date.setTime(timestamp);
            return date;
        }
        return null;
    }

 

posted @ 2016-06-14 19:09  rubekid  阅读(9060)  评论(1编辑  收藏  举报