Posted at

javascript:Dateにフォーマッター追加

More than 3 years have passed since last update.

prototype拡張して

Date.prototype.toFormattedString = function(format, weeks) { 

if (weeks == undefined) weeks = ["sun","mon","tue","wed","thu","fri","sat"];
return format.replace(/yyyy/g, this.getFullYear())
.replace(/MM/g, this.getMonth()+1)
.replace(/dd/g, this.getDate())
.replace(/E/g, weeks[this.getDay()]);};

呼び出す

var d = new Date();

console.log(d.toFormattedString("yyyy-MM-dd(E)"))

以上。