js时间转换事件

/*************************************** 数据类型转换相关 ***************************************/
    Convert: {
        dateToString: function (date) {
            if (date == null || date == undefined || date == "") {
                return "";
            }
            /// <summary>将日期转换成 yyyy-MM-dd 格式的字符串</summary>
            /// <param name="date" type="Date">日期对象</param>
            /// <returns type="String">转换后的字符串</returns>

            return date.getFullYear() + "-" + (date.getMonth() + 1).toString().fill(2, "0") + "-" + date.getDate().toString().fill(2, "0");
        },
        dateTimeToString: function (date) {
            if (date == null || date == undefined || date == "") {
                return "";
            }
            /// <summary>将日期转换成 yyyy-MM-dd 格式的字符串</summary>
            /// <param name="date" type="Date">日期对象</param>
            /// <returns type="String">转换后的字符串</returns>

            return date.getFullYear() + "-" + (date.getMonth() + 1).toString().fill(2, "0") + "-" + date.getDate().toString().fill(2, "0") + " " + date.getHours().toString().fill(2, "0") + ":" + date.getMinutes().toString().fill(2, "0") + ":" + date.getSeconds().toString().fill(2, "0");
        },
        stringToDate: function (str) {
            if (str == null || str == undefined || str == "") {
                return "";
            }
            /// <summary>将 yyyy-MM-dd 格式的日期字符串转换成Date对象</summary>
            /// <param name="str" type="String">要转换的字符串</param>
            /// <returns type="Date">日期对象</returns>

            var str = str.replace(/-/g, "/");
            return new Date(str);
        },
        jsonStrToDate: function (str) {
            if (str == null || str == undefined || str == "") {
                return "";
            }
            /// <summary>将 /Date(1334766600000)/ 格式的日期字符串转换成Date对象</summary>
            /// <param name="str" type="String">要转换的字符串</param>
            /// <returns type="Date">日期对象</returns>
            return eval("new " + str.replace(/\//g, ""));
        }
    }
}


/*************************************** String扩展 ***************************************/
String.prototype.fill = function (total, char) {
    /// <summary>将使用指定的字符将字符串填充到指定的长度</summary>
    /// <param name="total" type="Number">字符串的总长度</param>
    /// <param name="char" type="String">填充字符</param>
    /// <returns type="String">填充好的字符串</returns>

    var fillCount = total - this.length;
    if (fillCount <= 0) {
        return this;
    }
    var text = "";
    for (var i = 0; i < fillCount; i++) {
        text += char;
    }
    return text + this;
};

  

posted @ 2013-09-18 15:31  冰vs焰  阅读(346)  评论(0编辑  收藏  举报