


js date plug-in dateHelp gets the date of this month, three months, and this year_javascript skills
最近看了一些关于面向对象的知识,最近工作中在做统计查询的时候需要用到本月、近三个月、今年的日期范围,所以下面用用面向对象的思想写了一个获取日期的插件,大家可以借鉴使用。
直接通过new DateHelp就可以调用了
var myDate = new DateHelp({ date:'2015-02-01',//从此日期开始计算 format:'yyyy/MM/dd' }); myDate.getThisMonth(); myDate.getThreeMonth(); myDate.getThisYear();
dateHelp.js插件
/** * 通过调用可以获取本月,近三个月,今年的日期 * @param obj * @constructor */ function DateHelp(obj) { /*var obj = { date:'2015-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; 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 = 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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
