/** * Created by laixi on 2016/1/7. * Date對象擴展 */(function() { // 求當前日期與傳入的日期相隔多少天 if (typeof Date.prototype.getDateInterval != "function") { ...
/** * Created by laixi on 2016/1/7. * Date對象擴展 */ (function() { // 求當前日期與傳入的日期相隔多少天 if (typeof Date.prototype.getDateInterval != "function") { Date.prototype.getDateInterval = function(date) { var d = new Date(date); if (d == "Invalid Date") { throw "Invalid Date"; }else { // Math.abs 絕對值 return Math.abs(this*1-d*1)/60/60/1000/24; } } } // 求當前日期所在月的第一天 if (typeof Date.prototype.getFirstDateInMonth != "function") { Date.prototype.getFirstDateInMonth = function() { return new Date(this.getFullYear(), this.getMonth(), 1); } } // 求當前日期所在月的最後一天 if (typeof Date.prototype.getLastDateInMonth != "function") { Date.prototype.getLastDateInMonth = function() { return new Date(this.getFullYear(), this.getMonth()+1, 0); } } // 求當前日期所在季度的第一天 if (typeof Date.prototype.getFirstDateInQuarter != "function") { Date.prototype.getFirstDateInQuarter = function() { return new Date(this.getFullYear(), Math.floor(this.getMonth()/3)*3, 1); } } // 判斷是否為閏年 if (typeof Date.prototype.isLeapYear != "function") { Date.prototype.isLeapYear = function() { return new Date(this.getFullYear(), 2, 0).getDate() == 29; } } // 求某年某月的天數 if (typeof Date.daysInMonth != "function") { Date.daysInMonth = function(year, month) { var d = new Date(); d.setFullYear(year, (month == 12) ? 1 : month, 0); return d.getDate(); } } }());
參考
http://www.cnblogs.com/rubylouvre/archive/2010/09/16/1827784.html