/**
* 功能:返回用零补齐位数的字符串
* 参数:original为待补齐的源数字,num为补齐后的位数
*/
function patchZero(original, num){
var s = original.toString();
for(var i=0;i<num-s.length;i++){
s = "0" + s;
}
return(s);
}
/**
* 功能:返回格式化后的日期字符串
* 参数:formatStr为格式字符串,其中表示形式如下列表
* “ddd”- 汉字星期几
* “yyyy”- 四位数年份
* “MM”- 两位数月份
* “dd”- 两位数日期
* “hh”- 两位数小时
* “mm”- 两位数分钟
* “ss”- 两位数秒数
* “y”- 年份
* “M”- 月份
* “d”- 日期
* “h”- 小时
* “m”- 分钟
* “s”- 秒数
*/
Date.prototype.format = function (formatStr) {
var WEEK = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var s = formatStr;

s = s.replace(/d{3}/g, WEEK[this.getDay()]);

s = s.replace(/y{4}/g, patchZero(this.getFullYear(), 4));
s = s.replace(/M{2}/g, patchZero(this.getMonth() + 1, 2));
s = s.replace(/d{2}/g, patchZero(this.getDate(), 2));
s = s.replace(/h{2}/g, patchZero(this.getHours(), 2));
s = s.replace(/m{2}/g, patchZero(this.getMinutes(), 2));
s = s.replace(/s{2}/g, patchZero(this.getSeconds(), 2));

s = s.replace(/y{1}/g, this.getFullYear());
s = s.replace(/M{1}/g, this.getMonth() + 1);
s = s.replace(/d{1}/g, this.getDate());
s = s.replace(/h{1}/g, this.getHours());
s = s.replace(/m{1}/g, this.getMinutes());
s = s.replace(/s{1}/g, this.getSeconds());

return(s);
}var now = new Date();
alert(now.format("yyyy-MM-dd"));