Home > Web Front-end > JS Tutorial > Javascript powerful date function code sharing_javascript skills

Javascript powerful date function code sharing_javascript skills

WBOY
Release: 2016-05-16 17:23:27
Original
1089 people have browsed it
Copy code The code is as follows:

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;
            };
    } );
};
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template