js擴展
Date.prototype.toFormatString = function (formatString, isPad) {
/// <summary>
/// 格式化日期
/// <param>param1-String-日期格式 </param>
/// <param>param2-bool-是否補0,默認true </param>
/// </summary>
formatString = formatString || "yyyy-mm-dd";
isPad = isPad || true;
var date = this;
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var milliseconds = date.getMilliseconds();
if (isPad) {
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
milliseconds = milliseconds < 10 ? "0" + milliseconds : milliseconds;
}
switch (formatString) {
case "yyyy-mm-dd":
return year + "-" + month + "-" + day;
break;
case "yyyy-mm-dd hh:mm":
return year + "-" + month + "-" + day + " " + hours + ":" + minutes;
break;
case "yyyy-mm-dd hh:mm:ss":
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + milliseconds;
break;
case "yyyy/mm/dd":
return year + "/" + month + "/" + day;
break;
case "yyyy/mm/dd hh:mm":
return year + "/" + month + "/" + day + " " + hours + ":" + minutes;
break;
case "yyyy/mm/dd hh:mm:ss":
return year + "/" + month + "/" + day + " " + hours + ":" + minutes + ":" + milliseconds;
case "年月日":
return year + "年" + month + "月" + day + "日";
break;
case "年月日時分秒":
return year + "年" + month + "月" + day + "日" + hours + "時" + minutes + "分";
break;
default:
return year + "年" + month + "月" + day + "日" + hours + "時" + minutes + "分" + milliseconds + "秒";
break;
}
}
Date.prototype.parseJsonDate = function (jsonDate) {
/// <summary>
/// json日期格式轉換為日期對象
/// <param>param1-String-json日期 </param>
/// <param>return Date對象 </param>
/// </summary>
if (!jsonDate) return null;
var time = parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10);
var date = new Date(time);
return date;
}
var week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
Date.prototype.getWeek = function () {
return week[this.getDay()];
}
Date.prototype.addYear = function (value) {
if (!value) return this;
this.setFullYear(this.getFullYear() + value);
return this;
}
Date.prototype.addMonth = function (value) {
if (!value) return this;
this.setMonth(this.getMonth() + value);
return this;
}
Date.prototype.addDate = function (value) {
if (!value) return this;
this.setDate(this.getDate() + value);
return this;
}
js擴展