js extension of Date of Date into the specified format

// get the current time 
// Date of the extension, into the specified format Date String
// month (M), day (D), h (H), minutes (m), seconds (S), quarterly (q ) may be 1-2 placeholder
// years (y) may be 1-4 placeholder milliseconds (S) with only a placeholder (1-3 digits)
a Date. = function prototype.Format (FMT) {// author: meizz
var O = {
"M +": this.getMonth () +. 1, January //
"d +": this.getDate () , // day
"H +": this.getHours (), // h
"m +": this.getMinutes () , // points
"s +": this.getSeconds () , // seconds
"q +": Math.floor (( this.getMonth () + 3) / 3), // quarter
"S": this.getMilliseconds () // ms
};
IF (/(y+)/.test(fmt)) = fmt.replace FMT (the 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().Format("yyyy-MM-dd HH:mm:ss");
$('.datetime-top-right').html(time2);

Guess you like

Origin www.cnblogs.com/chengyalin/p/11264061.html