js日期轉換和格式化

1.日期格式化this

Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //
        "H+": this.getHours(), //小時 
        "m+": this.getMinutes(), //
        "s+": this.getSeconds(), //
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

調用:var time = new Date().Format("yyyy-MM-dd HH:mm:ss");spa

2.將指定日期轉換爲特定格式的日期
prototype

  var oldTime = (new Date("2016/12/20 20:11:11")).getTime();
    var curTime = new Date(oldTime).Format("yyyy-MM-dd HH:mm:ss");code

3.其餘更多格式化orm

  參考date相關方法本身任意設置格式。blog

相關文章
相關標籤/搜索