js时间戳转换为格式化日期

1、格式包括:

a. Y-m-d
b. Y-m-d H:i:s
c. Y年m月d日
d. Y年m月d日 H时i分

2、不传具体时间戳,默认是当前时间 timeatamp:时间戳,formats:时间格式。dateFormat(11111111111111, 'Y-m-d H:i:s')

            var dateFormat = function(timestamp, formats) {
                formats = formats || 'Y-m-d';
                var zero = function(value) {
                    if(value < 10) {
                        return '0' + value;
                    }
                    return value;
                };
                var myDate = timestamp ? new Date(timestamp) : new Date();
                var year = myDate.getFullYear();
                var month = zero(myDate.getMonth() + 1);
                var day = zero(myDate.getDate());

                var hour = zero(myDate.getHours());
                var minite = zero(myDate.getMinutes());
                var second = zero(myDate.getSeconds());

                return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
                    return({
                        Y: year,
                        m: month,
                        d: day,
                        H: hour,
                        i: minite,
                        s: second
                    })[matches];
                });
            };
与尘埃中开出花朵。
原文地址:https://www.cnblogs.com/congfeicong/p/8513661.html