JS将sql的dateTime格式数据例如:Wed Sep 30 00:00:00 CST 2020 字符串转换成2020-09-30 00:00:00时间格式

1.在标签内定义方法函数:

var format = function(time, format) {
            var t = new Date(time);
            var tf = function(i) {
                return (i < 10 ? '0' : '') + i
            };
            return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
                switch (a) {
                case 'yyyy':
                    return tf(t.getFullYear());
                    break;
                case 'MM':
                    return tf(t.getMonth() + 1);
                    break;
                case 'mm':
                    return tf(t.getMinutes());
                    break;
                case 'dd':
                    return tf(t.getDate());
                    break;
                case 'HH':
                    return tf(t.getHours());
                    break;
                case 'ss':
                    return tf(t.getSeconds());
                    break;
                }
            });
        }

2.在函数方法函数中调用方法:

var newDate = format(date, 'yyyy-MM-dd HH:mm:ss');// @param date:需要转换的时间如(Wed Sep 30 00:00:00 CST 2020)

console.log("newDate",newDate);

转换完成

你可能感兴趣的:(js,sql)