Javascript 日期格式化处理

 //日期处理
        Date.prototype.format = function (A) {
            var B = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "H+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S": this.getMilliseconds()
            };
            if (/(y+)/.test(A)) {
                A = A.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

                for (var C in B) {
                    if (new RegExp("(" + C + ")").test(A)) {
                        A = A.replace(RegExp.$1, RegExp.$1.length == 1 ? B[C] : ("00" + B[C]).substr(("" + B[C]).length));

                    }

                }
                return A;

            };
        }
        Date.prototype.dateDiff = function (C, D) {
            var B = 1;
            if (!C) {
                return "必须是日期对象";

            }
            var A = this.getTime() - C.getTime();
            switch (D) {
                case "ms":
                    B = 1;
                    break;
                case "s":
                    B = 1000;
                    break;
                case "m":
                    B = 60 * 1000;
                    break;
                case "h":
                    B = 60 * 60 * 1000;
                    break;
                case "d":
                    B = 24 * 60 * 60 * 1000;
                    break

            }
            return Math.floor(A / B);
        };
        Date.prototype.DateAdd = function (C, A) {
            var B = this;
            switch (C) {
                case "s":
                    return new Date(Date.parse(B) + (1000 * A));
                case "m":
                    return new Date(Date.parse(B) + (60000 * A));
                case "h":
                    return new Date(Date.parse(B) + (3600000 * A));
                case "d":
                    return new Date(Date.parse(B) + (86400000 * A));
                case "w":
                    return new Date(Date.parse(B) + ((86400000 * 7) * A));
                case "q":
                    return new Date(B.getFullYear(), (B.getMonth()) + A * 3, B.getDate(), B.getHours(), B.getMinutes(), B.getSeconds());
                case "M":
                    return new Date(B.getFullYear(), (B.getMonth()) + A, B.getDate(), B.getHours(), B.getMinutes(), B.getSeconds());
                case "y":
                    return new Date((B.getFullYear() + A), B.getMonth(), B.getDate(), B.getHours(), B.getMinutes(), B.getSeconds());

            }
            return B;
        };
        Date.prototype.Init = function (A) {
            return new Date(A.replace(/-/g, "/"));

        };
        Date.StrToDate = function (A) {
            return new Date(A.replace(/-/g, "/"));

        };
        Date.FormatTime = function (A) {
            var H = "";
            A = Math.round(A);
            var B = A % 60;
            if (B > 0) {
                H = B + "秒";
                A -= B;

            }
            if (A >= 60) {
                var J = A / 60;
                var E = J % 60;
                J -= E;
                H = E + "分" + H;
                if (J >= 60) {
                    var I = J / 60;
                    var C = I % 24;
                    I -= C;
                    H = C + "时" + H;
                    if (I >= 24) {
                        var L = I / 24;
                        var G = L % 30;
                        H = G + "天" + H;
                        L -= G;
                        if (L >= 30) {
                            var F = L / 30;
                            var D = F % 12;
                            H = D + "月" + H;
                            F -= D;
                            if (F >= 12) {
                                var K = F / 12;
                                H = K + "年" + H;

                            }

                        }

                    }

                }

            }
            return H;

        };

        function StrToDate(A) {
            return new Date(A.replace(/-/g, "/"));
        }
 

转载于:https://my.oschina.net/guanxinsui/blog/1538089

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值