Js date plug-in dateHelp to get the datetime _ javascript skills for this month, three months, and this year

Source: Internet
Author: User
This article mainly introduces the js date plug-in dateHelp to get the date of this month, three months, and this year, if you are interested, you can refer to the recently read some object-oriented knowledge. You need to use the date range of this month, the last three months, and this year to conduct statistical queries in your recent work, 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

 
   
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.