The js date plug-in dateHelp obtains the date of this month, three months, and this year, and jsdatehelp
I have recently read some object-oriented knowledge. In my recent work, we need to use the date range of this month, the last three months, and this year to perform statistical queries, so the following uses the object-oriented idea to write a plug-in for date acquisition. You can use it for reference.
You can call it directly through new DateHelp.
Var myDate = new DateHelp ({date: '1970-02-01 ', // calculate format: 'yyyy/MM/dd'} from this date); myDate. getThisMonth (); myDate. getThreeMonth (); myDate. getThisYear ();
DateHelp. js plug-in
/*** You can obtain the date of this month, last three months, and this year by calling * @ param obj * @ constructor */function DateHelp (obj) {/* var obj = {date: '1970-02-01 ', // calculate type: 'month' from this date, // calculate forward by year, month, and day: year ), month, day value: '14', // The value calculated forward, year, month, and day format: 'yyyy/mm/dd' // Date Format} */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'; // obtain the date, month, and day if (this. date instanceof Date) {// process this of the date function. year = this. date. getFullYear (); this. month = this. date. getMonth () + 1; this. day = this. date. getDate ();} else {// process the passed 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') {_ year-= _ value ;} else if (_ type = 'month' | _ type = 'month') {_ year-= parseInt (_ value/12 ); _ month-= _ value % 12; if (_ month <= 0) {_ year-= 1; _ month + = 12 ;}} else if (_ type = 'day' | _ type = 'day') {} else {} var date = new Date (_ year, _ month-1, _ day) re Turn this. formatDate (date, this. format);} DateHelp. prototype. formatDate = function (date, fmt) {var o = {"M +": date. getMonth () + 1, // month "d +": date. getDate (), // Day "h +": date. getHours (), // Hour "m +": date. getMinutes (), // minute "s +": date. getSeconds (), // second "q +": Math. floor (date. getMonth () + 3)/3), // quarter "S": date. getMilliseconds () // millisecond}; 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);}/* // example var myDate = new DateHelp ({date: '2017-02-01 ', // calculate format from this date: 'yyyy/MM/dd'}); console. log (myDate. getThisMonth (); console. log (myDate. getThreeMonth (); console. log (myDate. getThisYear ());*/
Html test code
<! DOCTYPE html>
I hope this article will help you learn about javascript programming.
Articles you may be interested in:
- A wave of JavaScript date judgment script sharing
- How to calculate the date difference in javascript
- Obtain the js Code of the lunar calendar and the current date.
- Native javascript implements automatic update time and date
- JS obtains time-related functions and converts timestamp to timestamp.
- Processing Method for displaying the date of last week and last month in javascript
- Js prototype Extension Method (string, date, Array Extension)
- Javascript: A simple method to compare the date size
- Summary of javascript date formatting Methods
- Js automatically generates the birth date based on the ID card number
- Use JavaScript to convert strings and dates and format them.