js将日期格式转化为YYYY-MM-DD HH:MM:SS

一、js将日期格式转化为YYYY-MM-DD HH:MM:SS 

Date.prototype.Format = function (fmt) { 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds() //秒 
    };
    if (/(y+)/.test(fmt)){ //根据y的长度来截取年
	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 time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date(1469281964000).Format("yyyy-MM-dd hh:mm:ss");

获取当前日期:

 getCurrentDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
          strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    },


//2018-12-1

https://www.cnblogs.com/wkrbky/p/6266701.html

你可能感兴趣的:(js练习)