Thursday, June 6, 2013

Javascript Date Formatter with PHP Format String

Every PHP programmer know how to format date using PHP built-in datetime functions or classes and whats the datetime format string to return the date object as date string. But when dealing with javascript I know there are lots of PHP programmer create custom function just to return date object as the function paramter and process the date object to return formatted date string. Thanks to phpjs.org, I created code as bellow to enable date formatting to all javascript Date object using prototype.

Date.prototype.format = function(str) {
    var d = this;
    var s = this.toString(), f = this.getTime();
    return ( '' + str ).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( str )
    {
        switch ( str )
        {
            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;
        }
    });
}
How to use :
var date = new Date('6/2/2013');
console.log(date.format('d-m-Y'));
// Output : 02-06-2013
console.log(date.format('Y-m-d'));
// Output : 2013-06-02
console.log(date.format('Y-m-d H:i:s'));
// Output : 2013-06-02 00:00:00
console.log(date.format('U'));
// Output : 1370106000
Hope that helps (:


Read more!