原生js 实现日期拆分 精确至时分秒

var date1='2018-12-26 00:00:00';
var date2='2018-12-29 04:00:00';
computed_time(date_sort(date1),date_sort(date2));
//日期拆分
function date_sort(date){
	var time={};
	var f = date.split(' ', 2);//过滤空格
            if(f[0].search("/") != -1){//判断是否包含-
                var d = (f[0] ? f[0] : '').split('/', 3);//过滤-
            }else {
                var d = (f[0] ? f[0] : '').split('-', 3);//过滤-
            }
            time.year=parseInt(d[0]);//转换成整数形式的原因是 过滤掉 月份和时分秒的首位补零的情况
            time.month=parseInt(d[1]);
            time.day=parseInt(d[2]);
            var t = (f[1] ? f[1] : '').split(':', 3);//过滤:
            time.hour=parseInt(t[0]);
            time.minute=parseInt(t[1]);
            time.second=parseInt(t[2]);
            return time;
}

猜你喜欢

转载自blog.csdn.net/u012967454/article/details/84869470