Skip to content

Instantly share code, notes, and snippets.

@NewFuture
Created September 8, 2017 05:04
Show Gist options
  • Save NewFuture/e041c2f10fa373fab2ebbba6be837e4e to your computer and use it in GitHub Desktop.
Save NewFuture/e041c2f10fa373fab2ebbba6be837e4e to your computer and use it in GitHub Desktop.
declare global {
interface Date {
format(fmt: string): string;
isToday(): boolean;
}
}
let week = ["一", "二", "三", "四", "五", "六", "日"];
let week_pre = ["", "周", "星期"];
Date.prototype.format = function (fmt: string): string {
var o = {
"M+": this.getMonth() + 1, //月份
"D+": this.getDate(), //日
"H+": this.getHours(), //24小时
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/([y|Y]{2,4})/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
if (/(d{1,3})/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (week_pre[RegExp.$1.length]) + week[this.getDay()]);
}
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;
}
Date.prototype.isToday = (): boolean => {
let today = new Date();
return today.getFullYear() === this.getFullYear() && today.getMonth() === this.getMonth() && today.getDate() === this.getDate();
};
export { };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment