js重写Date日期对象的add方法
来源:互联网 发布:教务系统的数据库设计 编辑:程序博客网 时间:2024/05/21 09:54
/** * 获取date所在月份的第一天。 * @returns {Date} */Date.prototype.getFirstDateOfMonth = function(){ return new Date(this.getFullYear(), this.getMonth(), 1);}/** * 获取date是月中的第几天 * @returns */Date.prototype.getDaysInMonth = function() { var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var m = this.getMonth(); return m == 1 && this.isLeapYear() ? 29 : daysInMonth[m];}/** * 获取date所在月份的最后一天 * @returns {Date} */Date.prototype.getLastDateOfMonth = function() { return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth());}/** * 重写日期add方法 * @returns {Date} */Date.prototype.add = function(interval, value){ var d = this; switch(interval) { case 'milli': d.setMilliseconds(this.getMilliseconds() + value); break; case 'second': d.setSeconds(this.getSeconds() + value); break; case 'minute': d.setMinutes(this.getMinutes() + value); break; case 'hour': d.setHours(this.getHours() + value); break; case 'day': d.setDate(this.getDate() + value); break; case 'month': var day = this.getDate(); if (day > 28) { day = Math.min(day, this.getFirstDateOfMonth().add('month', value).getLastDateOfMonth().getDate()); } d.setDate(day); d.setMonth(this.getMonth() + value); break; case 'year': d.setFullYear(this.getFullYear() + value); break; } return d;}
阅读全文
0 0
- js重写Date日期对象的add方法
- js将时间戳转换为日期以及js Date对象的一些方法
- JS中的日期(Date)对象
- JS:1.5日期(Date)对象
- JS Date(日期)对象
- 重写js的Date.format
- JavaScript Date日期对象方法
- js Date日期格式化方法
- javascript日期对象Date对象方法
- Date对象中处理时间和日期的常用方法
- javascript-Date对象 及 格式化日期的方法
- JavaScript 日期对象Date(声明/Date对象的方法/返回距离1970/01/01毫秒数)
- JS的date对象
- js的Date对象
- JS的Date对象
- JavaScript的日期Date对象
- js扩展Date对象 日期格式化
- 27 JS基础之--日期对象Date
- ES6对象扩展
- java基础
- Html a标签打开新页面
- CNN笔记:通俗理解卷积神经网络
- <button>和<input type="button">的区别
- js重写Date日期对象的add方法
- 2017 Multi-University Training Contest
- AR博物馆、景区实景导览工具、AR校园方案-十二居
- 关于String str="abc"与String str= new String("abc")区别
- maven
- ES6正则表达式扩展
- django中传参数给模板
- java写的AES文件加解密模块
- 深入分析java线程池的实现原理