⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.js

📁 ext-2.3.0
💻 JS
📖 第 1 页 / 共 4 页
字号:
    },    /**     * Get the numeric ISO-8601 week number of the year.     * (equivalent to the format specifier 'W', but without a leading zero).     * @return {Number} 1 to 53     */    getWeekOfYear : function() {        // adapted from http://www.merlyn.demon.co.uk/weekcalc.htm        var ms1d = 864e5, // milliseconds in a day            ms7d = 7 * ms1d; // milliseconds in a week        return function() { // return a closure so constants get calculated only once            var DC3 = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate() + 3) / ms1d, // an Absolute Day Number                AWN = Math.floor(DC3 / 7), // an Absolute Week Number                Wyr = new Date(AWN * ms7d).getUTCFullYear();            return AWN - Math.floor(Date.UTC(Wyr, 0, 7) / ms7d) + 1;        }    }(),    /**     * Checks if the current date falls within a leap year.     * @return {Boolean} True if the current date falls within a leap year, false otherwise.     */    isLeapYear : function() {        var year = this.getFullYear();        return !!((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));    },    /**     * Get the first day of the current month, adjusted for leap year.  The returned value     * is the numeric day index within the week (0-6) which can be used in conjunction with     * the {@link #monthNames} array to retrieve the textual day name.     * Example:     * <pre><code>var dt = new Date('1/10/2007');document.write(Date.dayNames[dt.getFirstDayOfMonth()]); //output: 'Monday'</code></pre>     * @return {Number} The day number (0-6).     */    getFirstDayOfMonth : function() {        var day = (this.getDay() - (this.getDate() - 1)) % 7;        return (day < 0) ? (day + 7) : day;    },    /**     * Get the last day of the current month, adjusted for leap year.  The returned value     * is the numeric day index within the week (0-6) which can be used in conjunction with     * the {@link #monthNames} array to retrieve the textual day name.     * Example:     * <pre><code>var dt = new Date('1/10/2007');document.write(Date.dayNames[dt.getLastDayOfMonth()]); //output: 'Wednesday'</code></pre>     * @return {Number} The day number (0-6).     */    getLastDayOfMonth : function() {        return this.getLastDateOfMonth().getDay();    },    /**     * Get the date of the first day of the month in which this date resides.     * @return {Date}     */    getFirstDateOfMonth : function() {        return new Date(this.getFullYear(), this.getMonth(), 1);    },    /**     * Get the date of the last day of the month in which this date resides.     * @return {Date}     */    getLastDateOfMonth : function() {        return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth());    },    /**     * Get the number of days in the current month, adjusted for leap year.     * @return {Number} The number of days in the month.     */    getDaysInMonth: function() {        var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];        return function() { // return a closure for efficiency            var m = this.getMonth();            return m == 1 && this.isLeapYear() ? 29 : daysInMonth[m];        }    }(),    /**     * Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').     * @return {String} 'st, 'nd', 'rd' or 'th'.     */    getSuffix : function() {        switch (this.getDate()) {            case 1:            case 21:            case 31:                return "st";            case 2:            case 22:                return "nd";            case 3:            case 23:                return "rd";            default:                return "th";        }    },    /**     * Creates and returns a new Date instance with the exact same date value as the called instance.     * Dates are copied and passed by reference, so if a copied date variable is modified later, the original     * variable will also be changed.  When the intention is to create a new variable that will not     * modify the original instance, you should create a clone.     *     * Example of correctly cloning a date:     * <pre><code>//wrong way:var orig = new Date('10/1/2006');var copy = orig;copy.setDate(5);document.write(orig);  //returns 'Thu Oct 05 2006'!//correct way:var orig = new Date('10/1/2006');var copy = orig.clone();copy.setDate(5);document.write(orig);  //returns 'Thu Oct 01 2006'</code></pre>     * @return {Date} The new Date instance.     */    clone : function() {        return new Date(this.getTime());    },    /**     * Checks if the current date is affected by Daylight Saving Time (DST).     * @return {Boolean} True if the current date is affected by DST.     */    isDST : function() {        // adapted from http://extjs.com/forum/showthread.php?p=247172#post247172        // courtesy of @geoffrey.mcgill        return new Date(this.getFullYear(), 0, 1).getTimezoneOffset() != this.getTimezoneOffset();    },    /**     * Attempts to clear all time information from this Date by setting the time to midnight of the same day,     * automatically adjusting for Daylight Saving Time (DST) where applicable.     * (note: DST timezone information for the browser's host operating system is assumed to be up-to-date)     * @param {Boolean} clone true to create a clone of this date, clear the time and return it (defaults to false).     * @return {Date} this or the clone.     */    clearTime : function(clone) {        if (clone) {            return this.clone().clearTime();        }        // get current date before clearing time        var d = this.getDate();        // clear time        this.setHours(0);        this.setMinutes(0);        this.setSeconds(0);        this.setMilliseconds(0);        if (this.getDate() != d) { // account for DST (i.e. day of month changed when setting hour = 0)            // note: DST adjustments are assumed to occur in multiples of 1 hour (this is almost always the case)            // refer to http://www.timeanddate.com/time/aboutdst.html for the (rare) exceptions to this rule            // increment hour until cloned date == current date            for (var hr = 1, c = this.add(Date.HOUR, hr); c.getDate() != d; hr++, c = this.add(Date.HOUR, hr));            this.setDate(d);            this.setHours(c.getHours());        }        return this;    },    /**     * Provides a convenient method for performing basic date arithmetic. This method     * does not modify the Date instance being called - it creates and returns     * a new Date instance containing the resulting date value.     *     * Examples:     * <pre><code>// Basic usage:var dt = new Date('10/29/2006').add(Date.DAY, 5);document.write(dt); //returns 'Fri Nov 03 2006 00:00:00'// Negative values will be subtracted:var dt2 = new Date('10/1/2006').add(Date.DAY, -5);document.write(dt2); //returns 'Tue Sep 26 2006 00:00:00'// You can even chain several calls together in one line:var dt3 = new Date('10/1/2006').add(Date.DAY, 5).add(Date.HOUR, 8).add(Date.MINUTE, -30);document.write(dt3); //returns 'Fri Oct 06 2006 07:30:00'</code></pre>     *     * @param {String} interval A valid date interval enum value.     * @param {Number} value The amount to add to the current date.     * @return {Date} The new Date instance.     */    add : function(interval, value) {        var d = this.clone();        if (!interval || value === 0) return d;        switch(interval.toLowerCase()) {            case Date.MILLI:                d.setMilliseconds(this.getMilliseconds() + value);                break;            case Date.SECOND:                d.setSeconds(this.getSeconds() + value);                break;            case Date.MINUTE:                d.setMinutes(this.getMinutes() + value);                break;            case Date.HOUR:                d.setHours(this.getHours() + value);                break;            case Date.DAY:                d.setDate(this.getDate() + value);                break;            case Date.MONTH:                var day = this.getDate();                if (day > 28) {                    day = Math.min(day, this.getFirstDateOfMonth().add('mo', value).getLastDateOfMonth().getDate());                }                d.setDate(day);                d.setMonth(this.getMonth() + value);                break;            case Date.YEAR:                d.setFullYear(this.getFullYear() + value);                break;        }        return d;    },    /**     * Checks if this date falls on or between the given start and end dates.     * @param {Date} start Start date     * @param {Date} end End date     * @return {Boolean} true if this date falls on or between the given start and end dates.     */    between : function(start, end) {        var t = this.getTime();        return start.getTime() <= t && t <= end.getTime();    }});/** * Formats a date given the supplied format string. * @param {String} format The format string. * @return {String} The formatted date. * @method format */Date.prototype.format = Date.prototype.dateFormat;// privateif (Ext.isSafari && (navigator.userAgent.match(/WebKit\/(\d+)/)[1] || NaN) < 420) {    Ext.apply(Date.prototype, {        _xMonth : Date.prototype.setMonth,        _xDate  : Date.prototype.setDate,        // Bug in Safari 1.3, 2.0 (WebKit build < 420)        // Date.setMonth does not work consistently if iMonth is not 0-11        setMonth : function(num) {            if (num <= -1) {                var n = Math.ceil(-num),                    back_year = Math.ceil(n / 12),                    month = (n % 12) ? 12 - n % 12 : 0;                this.setFullYear(this.getFullYear() - back_year);                return this._xMonth(month);            } else {                return this._xMonth(num);            }        },        // Bug in setDate() method (resolved in WebKit build 419.3, so to be safe we target Webkit builds < 420)        // The parameter for Date.setDate() is converted to a signed byte integer in Safari        // http://brianary.blogspot.com/2006/03/safari-date-bug.html        setDate : function(d) {            // use setTime() to workaround setDate() bug            // subtract current day of month in milliseconds, then add desired day of month in milliseconds            return this.setTime(this.getTime() - (this.getDate() - d) * 864e5);        }    });}/* Some basic Date tests... (requires Firebug)Date.parseDate('', 'c'); // call Date.parseDate() once to force computation of regex string so we can console.log() itconsole.log('Insane Regex for "c" format: %o', Date.parseCodes.c.s); // view the insane regex for the "c" format specifier// standard testsconsole.group('Standard Date.parseDate() Tests');    console.log('Date.parseDate("2009-01-05T11:38:56", "c")               = %o', Date.parseDate("2009-01-05T11:38:56", "c")); // assumes browser's timezone setting    console.log('Date.parseDate("2009-02-04T12:37:55.001000", "c")        = %o', Date.parseDate("2009-02-04T12:37:55.001000", "c")); // assumes browser's timezone setting    console.log('Date.parseDate("2009-03-03T13:36:54,101000Z", "c")       = %o', Date.parseDate("2009-03-03T13:36:54,101000Z", "c")); // UTC    console.log('Date.parseDate("2009-04-02T14:35:53.901000-0530", "c")   = %o', Date.parseDate("2009-04-02T14:35:53.901000-0530", "c")); // GMT-0530    console.log('Date.parseDate("2009-05-01T15:34:52,9876000+08:00", "c") = %o', Date.parseDate("2009-05-01T15:34:52,987600+08:00", "c")); // GMT+08:00console.groupEnd();// ISO-8601 format as specified in http://www.w3.org/TR/NOTE-datetime// -- accepts ALL 6 levels of date-time granularityconsole.group('ISO-8601 Granularity Test (see http://www.w3.org/TR/NOTE-datetime)');    console.log('Date.parseDate("1997", "c")                              = %o', Date.parseDate("1997", "c")); // YYYY (eg 1997)    console.log('Date.parseDate("1997-07", "c")                           = %o', Date.parseDate("1997-07", "c")); // YYYY-MM (eg 1997-07)    console.log('Date.parseDate("1997-07-16", "c")                        = %o', Date.parseDate("1997-07-16", "c")); // YYYY-MM-DD (eg 1997-07-16)    console.log('Date.parseDate("1997-07-16T19:20+01:00", "c")            = %o', Date.parseDate("1997-07-16T19:20+01:00", "c")); // YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)    console.log('Date.parseDate("1997-07-16T19:20:30+01:00", "c")         = %o', Date.parseDate("1997-07-16T19:20:30+01:00", "c")); // YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)    console.log('Date.parseDate("1997-07-16T19:20:30.45+01:00", "c")      = %o', Date.parseDate("1997-07-16T19:20:30.45+01:00", "c")); // YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)    console.log('Date.parseDate("1997-07-16 19:20:30.45+01:00", "c")      = %o', Date.parseDate("1997-07-16 19:20:30.45+01:00", "c")); // YYYY-MM-DD hh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)    console.log('Date.parseDate("1997-13-16T19:20:30.45+01:00", "c", true)= %o', Date.parseDate("1997-13-16T19:20:30.45+01:00", "c", true)); // strict date parsing with invalid month valueconsole.groupEnd();//*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -