"736c677c4" JS in the Date object's format operation class, named DateFormat, very easy to use __JS

Source: Internet
Author: User
Tags date1 dateformat getdate string format time interval
 
/** * This JS file is formatted JS in the date and time of the tool class, which contains the date object passed in, formatted into the desired format,<br> * or in the string format of the time, the second string date corresponding format can be converted to the corresponding Date object,<br> * You can calculate the difference between two dates * * y: for the Year * M: Represents the month of the year 1~12 * D: The number of days in the month 1~31 * H: Represents the number of hours in a day 00~23 * M: The number of minutes in the hour 00~59 *  
      S: The number of seconds in minutes 00~59/var DateFormat = function (bdebug) {this.isdebug = Bdebug | | false;  
  This.curdate = new Date (); } Dateformat.prototype = {//define some common date format constants Default_date_format: ' Yyyy-mm-dd ', default_month_for MAT: ' yyyy-mm ', Default_year_format: ' yyyy ', Default_time_format: ' HH:mm:ss ', Default_datetime_format: ' Yyyy-mm-dd HH:mm:ss ', Default_year: ' Year ', Default_month: ' MONTH ', default_date: ' DATE ', default_  
     HOUR: ' HOUR ', Default_minute: ' MINUTE ', Default_second: ' SECOND ',/** * Format the current date according to the given date time format * @params Strformat format string, such as: "YYYY-MM-DD" The default format is: "Yyyy-mm-dd HH:mm:ss" * @return returns the format of the time date that is represented by a string to a frame <br> * If an illegal format is passed in, the string format of the return date {@see date#tolocalestring ()} */Formatcurrentdate:function (strfor MAT) {try{var tempformat = Strformat = = undefined? this.  
          Default_datetime_format:strformat;  
          var dates = This.getdateobject (this.curdate);  
             if (/(y+)/.test (Tempformat)) {var fullyear = this.curDate.getFullYear () + '; var year = Regexp.$1.length = 4?  
             FullYear:fullYear.substr (4-regexp.$1.length);  
          Tempformat = Tempformat.replace (regexp.$1, year); for (var i in dates) {if (new RegExp (' + i + ') '). Test (Tempformat)) {var ta Rget = Regexp.$1.length = = 1?  
                Dates[i]: (' 0 ' + dates[i]). substr ((' + dates[i]). length-1);  
             Tempformat = Tempformat.replace (regexp.$1, target); } return Tempformat = = Strformat?  
       This.curDate.toLocaleString (): Tempformat;  
 }catch (e) {         This.debug (' Formatting date exception: ' + E.message '); },/** * Format the given time according to the given format * @params date to be formatted * @params Strformat to get 
     Format strings for dates, such as: ' Yyyy-mm-dd ', default: Yyyy-mm-dd HH:MM:SS * @return time format in accordance with the specified format * * @updateDate 2011-09-15 */format:function (date, Strformat) {try{if (date = = undefined) {this.curdate = n  
        EW Date (); }else if (!) (  
           Date instanceof date) {This.debug (' you entered date: ' + Date + ' is not a date type ');  
        return date;  
        }else{this.curdate = date;  
     Return This.formatcurrentdate (Strformat);  
     }catch (e) {this.debug (' Formatting date exception: ' + e.message);  
     },/** * resolves the given string datetime according to the given format, * @params strdate A string representation of the date to be resolved, this parameter can only be a string-form date, otherwise the current system date is returned * @params Strformat Resolve the order of the given date, if the input strdate format is in the format supported by the {Date.parse ()} method,<br> * Can not be passed in, otherwise it must be passed in with STRD Ate the corresponding format, the current system date is returned if the format is not passed in. 
     * @return Returns the time of the resolved date type <br> * If it cannot be resolved, return the current date <br> * If given the time format, the date returned is January 1, 1970 * * BUG: This method can only implement a date that is similar to the ' YYYY-MM-DD ' format,<br> * and ' yyyyMMdd ' in the form of a date that cannot be implemented * * p  
       Arsedate:function (strdate, Strformat) {if (typeof strdate!= ' string ') {return new Date ();  
      var longtime = Date.parse (strdate);  
             if (isNaN (longtime)) {if (Strformat = = undefined) {this.debug (' Please enter a date format ');  
          return new Date ();  
          var tmpdate = new Date (); var Regformat =/(\w{4}) | (\w{2}) |  
          (\w{1})/g; var regdate =/(\d{4}) | (\d{2}) |  
          (\d{1})/g;  
          var formats = Strformat.match (Regformat);  
          var dates = Strdate.match (regdate); if (formats!= undefined && dates!= undefined && formats.length = = dates.length) {for (Var i = 0; I < formats. length;  
              i++) {var format = formats[i];  
              if (format = = = ' yyyy ') {tmpdate.setfullyear (parseint (dates[i), 10));  
                }else if (format = = ' yy ') {var prefix = (tmpdate.getfullyear () + '). Substring (0, 2); var year = (parseint (dates[i], ten) + '). Length = = 4?  
                parseint (Dates[i]: prefix + (parseint (Dates[i], m) + ') substring (0, 2);  
                var tmpyear = parseint (year, 10);  
              Tmpdate.setfullyear (tmpyear);  
              }else if (format = = ' MM ' | | | | format = = ' M ') {tmpdate.setmonth (parseint (dates[i), 10)-1);  
              }else if (format = = ' DD ' | |: Format = = ' d ') {Tmpdate.setdate (parseint (dates[i), 10));  
              }else if (format = = ' HH ' | |-format = = ' H ') {tmpdate.sethours (parseint (dates[i), 10)); }else if (format = = ' mm ' | | |-format = = ' m ') {tmpdate.setminutes (parseint) (dateS[i], 10));  
              }else if (format = = ' ss ' | |: Format = = ' s ') {tmpdate.setseconds (parseint (dates[i), 10));  
         } return tmpdate;  
        return tmpdate;  
        }else{return new Date (longtime); },/** * calculates and formats a given time in a given format, based on the given interval type and interval value, and returns * @params date time to be manipulated is a string of time or {@see Date} A similar time object, * @params interval interval types such as "year", "MONTH", "Date", case-insensitive * @params amount time interval values, positive and negative numbers, A negative number subtracts the corresponding value from the date, and a positive number adds the corresponding value to date @params Strformat when the date in the input is formatted as a string, this entry must be entered. 
     If the date parameter is {@see Date} type is the format for which this item will be the final output.   
    * @params Targetformat The date and time of the final output, if no input is used Strformat or default format ' Yyyy-mm-dd HH:mm:ss ' * @return returns the string of the calculated and formatted time * *  
        Changedate:function (date, interval, amount, Strformat, Targetformat) {var tmpdate = new Date ();  
           if (date = = undefined) {this.debug (' input time cannot be empty! ');return new Date ();  
        }else if (typeof date = = ' string ') {tmpdate = This.parsedate (date, strformat);  
        }else if (date instanceof date) {tmpdate = date; var field = (typeof interval = = ' string ')?  
          
        Interval.touppercase (): ' DATE ';  
          try{amount = parseint (amount + ', 10);  
          if (isNaN (amount)) {amount = 0;  
           }}catch (e) {this.debug (' you entered [amount= ' + amount + '] cannot be converted to an integer ');  
        Amount = 0; Switch (field) {case this.  
             DEFAULT_YEAR:tmpdate.setFullYear (tmpdate.getfullyear () + amount);  
           Break Case this.  
             DEFAULT_MONTH:tmpdate.setMonth (Tmpdate.getmonth () + amount);  
           Break Case this.  
             DEFAULT_DATE:tmpdate.setDate (tmpdate.getdate () + amount);  
           Break Case this.  
           Default_hour:  Tmpdate.sethours (tmpdate.gethours () + amount);  
           Break Case this.  
             DEFAULT_MINUTE:tmpdate.setMinutes (tmpdate.getminutes () + amount);  
           Break Case this.  
             DEFAULT_SECOND:tmpdate.setSeconds (tmpdate.getseconds () + amount);  
           Break          
        Default:this.debug (' You enter [interval: ' + field + '] does not meet the conditions! ');  
        } this.curdate = Tmpdate;  
    Return This.formatcurrentdate (Targetformat = = undefined? strformat:targetformat); /** * Compares two date gaps * @param date1 Date type Time * @param date2 dete type of time * @param isform 
     At Boolean to format the resulting time,<br> * false: Returns the number of milliseconds, true: Returns the formatted data * @return Returns the number of milliseconds between two dates or the result of formatting  
            * * Compareto:function (date1, Date2, Isformat) {try{var len = arguments.length;  
            var tmpdate1 = new Date (); var tmpdate2 = new Date ();  
            if (len = = 1) {tmpdate1 = Date1;  
              }else if (len >= 2) {tmpdate1 = Date1;  
            Tmpdate2 = Date2; } if (! ( Tmpdate1 instanceof Date) | | ! (Tmpdate2 instanceof Date))  
        {return 0;   
            }else{var time1 = Tmpdate1.gettime ();  
            var time2 = Tmpdate2.gettime ();  
            var time = Math.max (time1, Time2)-Math.min (time1, time2);  
                  if (!isnan) && time > 0 {if (isformat) {var date = new Date (time);  
                  var result = {}; result[' year '] = (Date.getfullyear ()-1970) > 0?  
                  (Date.getfullyear ()-1970): ' 0 '; result[' Month ' = (Date.getmonth ()-1) > 0?  
                  (Date.getmonth ()-1): ' 0 '; result[' Day ' = (Date.getdate ()-1) > 0?  
                  (Date.getdate ()-1): ' 0 '; Result[' hour '] = (date.getHours ()-8) > 0?  
                  (Date.gethours ()-1): ' 0 '; result[' minute '] = date.getminutes () > 0?  
                  Date.getminutes (): ' 0 '; result[' second '] = date.getseconds () > 0?  
                  Date.getseconds (): ' 0 ';  
                return result;  
                }else {return time;  
            }}else{return 0;  
      }}catch (e) {this.debug (' Abnormal time exception ' + e.message); },/** * The date of the month, day, time, minute, and second of the given date the object * @params date is a non-date type, get the current date * @ret A URN has an object of month, day, time, minutes, and seconds of a given date/getdateobject:function (date) {if (!)  
         Date instanceof date) {date = new date ();  return {' m+ ': date.getmonth () + 1, ' d+ ': date.getdate (), ' h+ '  
         : Date.gethours (), ' m+ ': date.getminutes (), ' s+ ': Date.getseconds ()};   
           /** * In the console output log * @params message to output log information * * Debug:function (Messages) {try{  
           if (!this.isdebug) {return;  
               } if (!window.console) {window.console = {};  
               Window.console.log = function () {return;  
       } window.console.log (message + ');  
 }catch (e) {}}}

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.