js日期插件dateHelp獲取本月、三個月、今年的日期
最近看了一些關(guān)于面向?qū)ο蟮闹R,最近工作中在做統(tǒng)計查詢的時候需要用到本月、近三個月、今年的日期范圍,所以下面用用面向?qū)ο蟮乃枷雽懥艘粋€獲取日期的插件,大家可以借鑒使用。
直接通過new DateHelp就可以調(diào)用了
var myDate = new DateHelp({ date:'2015-02-01',//從此日期開始計算 format:'yyyy/MM/dd' }); myDate.getThisMonth(); myDate.getThreeMonth(); myDate.getThisYear();
dateHelp.js插件
/** * 通過調(diào)用可以獲取本月,近三個月,今年的日期 * @param obj * @constructor */ function DateHelp(obj) { /*var obj = { date:'2015-02-01',//從此日期開始計算 type:'month',//以年月日向前計算:年(year),月(month),日(day) value:'14',//向前計算的數(shù)值,年月日 format:'yyyy/mm/dd'//日期格式 }*/ this.date = obj.date; this.type = obj.type; this.value = obj.value == undefined ? obj.value : 0; this.format = obj.format == undefined ? obj.format: 'yyyy/MM/dd'; //日期和非日期格式獲取年月日 if (this.date instanceof Date){ //處理傳進來的是日期函數(shù)的 this.year = this.date.getFullYear(); this.month = this.date.getMonth()+1; this.day = this.date.getDate(); }else{ //處理傳入的是非日期函數(shù)的 this.year = this.date.substr(0, 4); this.month = this.date.substr(5, 2); this.day = this.date.substr(8, 2); } } DateHelp.prototype.beforeDate = function(type, value){ var _type = type || this.type, _value = value || this.value, _year = this.year, _month = this.month, _day = this.day; if (_type == 'year' || _type == '年'){ _year -= _value; }else if (_type == 'month' || _type == '月'){ _year -= parseInt(_value / 12); _month -= _value % 12; if(_month <= 0){ _year -= 1; _month += 12; } }else if (_type == 'day' || _type == '日'){ }else { } var date = new Date(_year, _month - 1, _day) return this.formatDate(date, this.format); } DateHelp.prototype.formatDate = function(date,fmt){ var o = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小時 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "q+" : Math.floor((date.getMonth()+3)/3), //季度 "S" : date.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); 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; } DateHelp.prototype.getThisMonth = function() { var first = new Date(this.year, this.month - 1); var last = new Date(this.year, this.month, 0); return this.formatDate(first, this.format) + " - " + this.formatDate(last, this.format); } DateHelp.prototype.getThreeMonth = function() { return this.beforeDate('month', 3) + " - " + this.beforeDate('day', 0); } DateHelp.prototype.getThisYear = function() { var first = new Date(this.year, 0, 1); var last = new Date(this.year, 11, 31); return this.formatDate(first, this.format) + " - " + this.formatDate(last, this.format); } /* //示例 var myDate = new DateHelp({ date:'2015-02-01',//從此日期開始計算 format:'yyyy/MM/dd' }); console.log(myDate.getThisMonth()); console.log(myDate.getThreeMonth()); console.log(myDate.getThisYear());*/
html測試代碼
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="myJs/dateHelp.js"></script> </head> <body> <script> var myDate = new DateHelp({ date:new Date(),//從此日期開始計算 format:'yyyy/MM/dd' }); console.log(myDate.getThisMonth()); console.log(myDate.getThreeMonth()); console.log(myDate.getThisYear()); </script> </body> </html>
希望本文所述對大家學習javascript程序設計有所幫助。
相關(guān)文章
Javascript表達式中連續(xù)的 && 和 || 之賦值區(qū)別
了區(qū)分賦值表達式中出現(xiàn)的連續(xù)的 ‘&&’和 ‘||’的不同的賦值含義,做了一個小測試.2010-10-10js獲取URL的參數(shù)的方法(getQueryString)示例
getQueryString方法默認返回的是 string如果是int類型,則JS使用的時候,要進行轉(zhuǎn)換一下,下面有個不錯的示例,大家可以參考下2013-09-09javascript 產(chǎn)生隨機數(shù)的幾種方法總結(jié)
這篇文章主要介紹了javascript 產(chǎn)生隨機數(shù)的幾種方法總結(jié)的相關(guān)資料,希望通過本文大家能夠掌握如何實現(xiàn)這樣的方法,需要的朋友可以參考下2017-09-09JavaScript?中的單例內(nèi)置對象Global?與?Math
這篇文章主要介紹了JavaScript?中的單例內(nèi)置對象Global與Math,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07使用JavaScript?將數(shù)據(jù)網(wǎng)格綁定到?GraphQL?服務的操作方法
GraphQL是管理JavaScript應用程序中數(shù)據(jù)的優(yōu)秀工具,本教程展示了GraphQL和SpreadJS如何簡單地構(gòu)建應用程序,?GraphQL?和?SpreadJS都有更多功能可供探索,因此您可以做的事情遠遠超出了這個示例,感興趣的朋友一起看看吧2023-11-11JavaScript使用shift方法移除素組第一個元素實例分析
這篇文章主要介紹了JavaScript使用shift方法移除素組第一個元素的用法,實例分析了javascript中shift方法的使用技巧,需要的朋友可以參考下2015-04-04