美文网首页
2019-03-14

2019-03-14

作者: 青木川_ | 来源:发表于2019-03-14 18:21 被阅读0次

Date.prototype.format = function (style) {

    /// <summary>格式化日期</summary>

    /// <param name="name" type="string">格式:例yyyy-MM-dd</param>

    /// <returns type="Date">返回日期</returns>

    var o = {

        "Y+": this.getFullYear(), //month

        "M+": this.getMonth() + 1, //month

        "d+": this.getDate(),      //day

        "h+": this.getHours(),    //hour

        "m+": this.getMinutes(),  //minute

        "s+": this.getSeconds(),  //secondsss

        "w+": "日一二三四五六".charAt(this.getDay()),  //week

        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter

        "S": this.getMilliseconds() //millisecond

    }

    if (/(y+)/.test(style))

        style = style.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

    for (var k in o) {

        if (new RegExp("(" + k + ")").test(style)) style = style.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));

    }

    return style;

}

String.prototype.toTime = function () {

    /// <summary>字符串转日期</summary>

    /// <returns type="Date">返回日期</returns>

    var val = this.match(/\d+|\D+/g);

    var st = "";

    if (val.length == 11 || val.length == 13)

        st = val[0] + "/" + val[2] + "/" + val[4] + " " + val[6] + ":" + val[8] + ":" + val[10];

    else if (val.length == 5)

        st = val[0] + "/" + val[2] + "/" + val[4];

    var dt = Date.parse(st)

    return new Date(dt);

}

相关文章

网友评论

      本文标题:2019-03-14

      本文链接:https://www.haomeiwen.com/subject/foicmqtx.html