Skip to content

Instantly share code, notes, and snippets.

@marsen
Created October 16, 2014 03:21
Show Gist options
  • Save marsen/ba59f6a420ac99237f99 to your computer and use it in GitHub Desktop.
Save marsen/ba59f6a420ac99237f99 to your computer and use it in GitHub Desktop.
DateFormat.js
//擴充Date基本方法
Date.prototype.format = function (fmt) {
var opt = {
"M+": this.getMonth() + 1, //月
"d+": this.getDate(), //日
"h+": this.getHours(), //時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var o in opt) {
if (new RegExp("(" + o + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (opt[o]) : (("00" + opt[o]).substr(("" + opt[o]).length)));
}
return fmt;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment