js日期插件dateHelp猎取本月、三个月、今年的日期_.docx

上传人:PIYPING 文档编号:11633438 上传时间:2021-08-27 格式:DOCX 页数:7 大小:13.05KB
返回 下载 相关 举报
js日期插件dateHelp猎取本月、三个月、今年的日期_.docx_第1页
第1页 / 共7页
js日期插件dateHelp猎取本月、三个月、今年的日期_.docx_第2页
第2页 / 共7页
js日期插件dateHelp猎取本月、三个月、今年的日期_.docx_第3页
第3页 / 共7页
js日期插件dateHelp猎取本月、三个月、今年的日期_.docx_第4页
第4页 / 共7页
js日期插件dateHelp猎取本月、三个月、今年的日期_.docx_第5页
第5页 / 共7页
亲,该文档总共7页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《js日期插件dateHelp猎取本月、三个月、今年的日期_.docx》由会员分享,可在线阅读,更多相关《js日期插件dateHelp猎取本月、三个月、今年的日期_.docx(7页珍藏版)》请在三一文库上搜索。

1、js日期插件dateHelp猎取本月、三个月、今年的日期_ 最近看了一些关于面对对象的学问,最近工作中在做统计查询的时候需要用到本月、近三个月、今年的日期范围,所以下面用用面对对象的思想写了一个猎取日期的插件,大家可以借鉴用法。 挺直通过new DateHelp就可以调用了 var myDate = new DateHelp( date:2021-02-01,/从今日期开头计算 format:yyyy/MM/dd ); myDate.getThisMonth(); myDate.getThreeMonth(); myDate.getThisYear(); dateHelp.js插件 /* *

2、通过调用可以猎取本月,近三个月,今年的日期 * param obj * constructor */ function DateHelp(obj) /*var obj = date:2021-02-01,/从今日期开头计算 type:month,/以年月日向前计算:年(year),月(month),日(day) value:14,/向前计算的数值,年月日 format:yyyy/mm/dd/日期格式 */ this.date = obj.date; this.type = obj.type; this.value = obj.value = undefined ? obj.value : 0;

3、 this.format = obj.format = undefined ? obj.format: yyyy/MM/dd; /日期和非日期格式猎取年月日 if (this.date instanceof Date) /处理传进来的是日期函数的 this.year = this.date.getFullYear(); this.month = this.date.getMonth()+1; this.day = this.date.getDate(); else /处理传入的是非日期函数的 this.year = this.date.substr(0, 4); this.month = th

4、is.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 | _

5、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+ : dat

6、e.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); f

7、or(var k in o) if(new RegExp(+ k +).test(fmt) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length=1) ? (ok) : (00+ ok).substr(+ ok).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

8、.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

9、, 31); return this.formatDate(first, this.format) + - + this.formatDate(last, this.format); /* /示例 var myDate = new DateHelp( date:2021-02-01,/从今日期开头计算 format:yyyy/MM/dd ); console.log(myDate.getThisMonth(); console.log(myDate.getThreeMonth(); console.log(myDate.getThisYear();*/ html测试代码 !DOCTYPE ht

10、ml 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程序设计有所关心。 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1