javascript強大的日期函數代碼分享_javascript技巧

來源:互聯網
上載者:User
複製代碼 代碼如下:

var date = function( a, s ) {
    var d = s ? new Date( s ) : new Date(), f = d.getTime();
    return ( '' + a ).replace( /a|A|d|D|F|g|G|h|H|i|I|j|l|L|m|M|n|s|S|t|T|U|w|y|Y|z|Z/g, function ( a ) {
        switch ( a ) {
                case 'a' : return d.getHours() > 11 ? 'pm' : 'am';
                case 'A' : return d.getHours() > 11 ? 'PM' : 'AM';
                case 'd' : return ( '0' + d.getDate() ).slice(-2);
                case 'D' : return [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ][ d.getDay() ];
                case 'F' : return [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ][ d.getMonth() ];
                case 'g' : return ( s = ( d.getHours() || 12 ) ) > 12 ? s - 12 : s;
                case 'G' : return d.getHours();
                case 'h' : return ( '0' + ( ( s = d.getHours() || 12 ) > 12 ? s - 12 : s ) ).slice(-2);
                case 'H' : return ( '0' + d.getHours() ).slice(-2);
                case 'i' : return ( '0' + d.getMinutes() ).slice(-2);
                case 'I' : return (function(){ d.setDate(1); d.setMonth(0); s = [ d.getTimezoneOffset() ]; d.setMonth(6); s[1] = d.getTimezoneOffset(); d.setTime( f ); return s[0] == s[1] ? 0 : 1; })();
                case 'j' : return d.getDate();
                case 'l' : return [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ][ d.getDay() ];
                case 'L' : return ( s = d.getFullYear() ) % 4 == 0 && ( s % 100 != 0 || s % 400 == 0 ) ? 1 : 0;
                case 'm' : return ( '0' + ( d.getMonth() + 1 ) ).slice(-2);
                case 'M' : return [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ][ d.getMonth() ];
                case 'n' : return d.getMonth() + 1;
                case 's' : return ( '0' + d.getSeconds() ).slice(-2);
                case 'S' : return [ 'th', 'st', 'nd', 'rd' ][ ( s = d.getDate() ) < 4 ? s : 0 ];
                case 't' : return (function(){ d.setDate(32); s = 32 - d.getDate(); d.setTime( f ); return s; })();
                case 'T' : return 'UTC';
                case 'U' : return ( '' + f ).slice( 0, -3 );
                case 'w' : return d.getDay();
                case 'y' : return ( '' + d.getFullYear() ).slice(-2);
                case 'Y' : return d.getFullYear();
                case 'z' : return (function(){ d.setMonth(0); return d.setTime( f - d.setDate(1) ) / 86400000; })();
                default : return -d.getTimezoneOffset() * 60;
            };
    } );
};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.