// 添加秒数 Date.prototype.addSecond = function (count) { var time = this.getTime(); time += count * 1000; return new Date(time); } // 添加分钟 Date.prototype.addMinute = function (count) { return this.addSecond(count * 60); } // 添加小时 Date.prototype.addHour = function (count) { return this.addMinute(count * 60); } // 添加天数 Date.prototype.addDay = function (count) { return this.addHour(count * 24); } // 添加月份 Date.prototype.addMonth = function (count) { var year = this.getFullYear(); var month = this.getMonth(); var date = this.getDate(); // 计算年份 year += Math.floor((month + count) / 12); // 计算月份 month = Math.floor((month + count) % 12); if (month <= 0) { month += 12; } // 计算天数 var dateMax = new Date(`${year}/${month + 1}/0`).getDate(); if (date > dateMax) { date = dateMax } return new Date(year, month, date, this.getHours(), this.getMinutes(), this.getSeconds()); } // 添加年份 Date.prototype.addYear = function (count) { return this.addMonth(count * 12); } // 计算两个日期数据,相隔的天数 Date.prototype.dayDiff = function (date) { if (!date) { return 0; } var time = this.getTime(); var dateTime = date.getTime(); var timeDiff = time - dateTime; var dayDiff = timeDiff / (1000 * 60 * 60 * 24); return dayDiff; } // 判断日期是否是今天 Date.prototype.isToday = function (str) { let d = new Date(str).setHours(0, 0, 0, 0); let today = new Date().setHours(0, 0, 0, 0); let obj = { '-86400000': '昨天', 0: '今天', 86400000: '明天', }; return obj[d - today] || false; } // 返回格式化的日期字符串,默认yyyy-MM-dd格式 // yyyy:4位数的年份 // MM:2位数的月份 // dd:2位数的日 // HH:2位数的小时数,按24小时制 // mm:2位数的分钟数 // ss:2位数的秒钟数 // ww:星期数 // w:星期数(简) Date.prototype.format = function (fmt) { fmt = fmt || 'yyyy-MM-dd'; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)) } if (this.getFullYear() <= 1900) { return ''; } let keys = { 'yyyy': this.getFullYear(), 'MM': this.getMonth() + 1, 'dd': this.getDate(), 'HH': this.getHours(), 'mm': this.getMinutes(), 'ss': this.getSeconds(), 'ww': ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][this.getDay()], 'w': ['日', '一', '二', '三', '四', '五', '六'][this.getDay()] } for (let key in keys) { if (new RegExp(`(${key})`).test(fmt)) { let value = keys[key] < 10 ? ('0' + keys[key]) : keys[key]; fmt = fmt.replace(RegExp.$1, value) } } return fmt } // 判断是否当天,若是当天则返回[时:分],若不是当天则返回[月-日] Date.prototype.toSmartString = function () { let time = this; // console.log('time', time); if (!time) return ''; var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hour = time.getHours(); var minute = time.getMinutes(); var now = new Date(); var nowYear = now.getFullYear(); var nowMonth = now.getMonth() + 1; var nowDate = now.getDate(); if (nowYear == year && nowMonth == month && nowDate == date) { return `${hour.padStart(2,'0')}:${minute.padStart(2,'0')}`; // 对Number类型扩展了padStart函数,不满2位数的自动补齐 } else if (nowYear == year) { return `${month.padStart(2,'0')}-${date.padStart(2,'0')}`; } else { return `${year.padStart(4,'0')}-${month.padStart(2,'0')}-${date.padStart(2,'0')}`; } }