Advertisement
Guest User

Data.prototype.format

a guest
Jun 14th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.     if(!Date.prototype.format) {
  3.    
  4.         //Formats a JavaScript Date object to a string.
  5.         //Following PHP's date() format --
  6.         //http://php.net/manual/en/function.date.php
  7.         Date.prototype.format = (function () {
  8.        
  9.             var pattern = {
  10.                
  11.                     //Day of the month, 2 digits with leading zeros
  12.                     d: function () {
  13.                         var day = this.getDate();
  14.                         return day < 10 ? '0' + day : day;
  15.                     },
  16.                    
  17.                     //A textual representation of a day, three letters
  18.                     D: function () {
  19.                         return pattern['l'].call(this).slice(0,3);
  20.                     },
  21.                    
  22.                     //Day of the month without leading zeros
  23.                     j: function () {
  24.                         return this.getDate();
  25.                     },
  26.                    
  27.                     //A full textual representation of the day of the week
  28.                     l: function () {
  29.                         switch(this.getDay()) {
  30.                         case 0:
  31.                             return 'Sunday';
  32.                         case 1:
  33.                             return 'Monday';
  34.                         case 2:
  35.                             return 'Tuesday';
  36.                         case 3:
  37.                             return 'Wednesday';
  38.                         case 4:
  39.                             return 'Thursday';
  40.                         case 5:
  41.                             return 'Friday';
  42.                         case 6:
  43.                             return 'Saturday';
  44.                         }
  45.                     },
  46.                    
  47.                     //ISO-8601 numeric representation of the day of the week
  48.                     N: function () {
  49.                         return this.getDay() === 0 ? 7 : this.getDay();
  50.                     },
  51.                    
  52.                     //English ordinal suffix for the day of the month, 2 characters
  53.                     S: function () {
  54.                    
  55.                         if(this.getDate() > 3 && this.getDate() < 21) {
  56.                             return 'th';
  57.                         }
  58.                    
  59.                         switch(this.getDate().toString().slice(-1)) {
  60.                         case '1':
  61.                             return 'st';
  62.                         case '2':
  63.                             return 'nd';
  64.                         case '3':
  65.                             return 'rd';
  66.                         default:
  67.                             return 'th';
  68.                         }
  69.                     },
  70.                    
  71.                     //Numeric representation of the day of the week
  72.                     w: function () {
  73.                         return this.getDay();
  74.                     },
  75.                    
  76.                     //The day of the year (starting from 0)
  77.                     z: function () {
  78.                         return Math.floor(((this - new Date(this.getFullYear(), 0, 1)) / 86400000), 0);
  79.                     },
  80.                    
  81.                     //ISO-8601 week number of year, weeks starting on Monday
  82.                     W: function () {
  83.                         var start = new Date(this.getFullYear(), 0, 1);
  84.                         return Math.ceil((((this - start) / 86400000) + start.getDay() + 1) / 7);
  85.                     },
  86.                    
  87.                     //A full textual representation of a month, such as January or March
  88.                     F: function () {
  89.                         switch(this.getMonth()) {
  90.                         case 0:
  91.                             return 'January';
  92.                         case 1:
  93.                             return 'February';
  94.                         case 2:
  95.                             return 'March';
  96.                         case 3:
  97.                             return 'April';
  98.                         case 4:
  99.                             return 'May';
  100.                         case 5:
  101.                             return 'June';
  102.                         case 6:
  103.                             return 'July';
  104.                         case 7:
  105.                             return 'August';
  106.                         case 8:
  107.                             return 'September';
  108.                         case 9:
  109.                             return 'October';
  110.                         case 10:
  111.                             return 'November';
  112.                         case 11:
  113.                             return 'December';
  114.                         }
  115.                     },
  116.                    
  117.                     //Numeric representation of a month, with leading zeros
  118.                     m: function () {
  119.                         var month = this.getMonth()+1;
  120.                         return month < 10 ? '0' + month : month;
  121.                     },
  122.                    
  123.                     //A short textual representation of a month, three letters
  124.                     M: function () {
  125.                         return pattern['F'].call(this).slice(0,3);
  126.                     },
  127.                    
  128.                     //Numeric representation of a month, without leading zeros
  129.                     n: function () {
  130.                         return this.getMonth()+1;
  131.                     },
  132.                    
  133.                     //Number of days in the given month
  134.                     t: function () {
  135.                         return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate();
  136.                     },
  137.                    
  138.                     //Whether it's a leap year
  139.                     L: function () {
  140.                         return new Date(this.getFullYear(), 1, 29).getDate() === 29 ? 1 : 0;
  141.                     },
  142.                    
  143.                     //ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
  144.                     O: function () {
  145.                         return null;
  146.                     },
  147.                    
  148.                     //A full numeric representation of a year, 4 digits
  149.                     Y: function () {
  150.                         return this.getFullYear();
  151.                     },
  152.                    
  153.                     //A two digit representation of a year
  154.                     y: function () {
  155.                         return this.getFullYear().toString().slice(-2);
  156.                     },
  157.                    
  158.                     //Lowercase Ante meridiem and Post meridiem
  159.                     a: function () {
  160.                         return this.getHours() < 12 ? 'am' : 'pm';
  161.                     },
  162.                    
  163.                     //Uppercase Ante meridiem and Post meridiem
  164.                     A: function () {
  165.                         return this.getHours() < 12 ? 'AM' : 'PM';
  166.                     },
  167.                    
  168.                     //Swatch Internet time
  169.                     B: function () {
  170.                         return null;
  171.                     },
  172.                    
  173.                     //12-hour format of an hour without leading zeros
  174.                     g: function () {
  175.                         var hours = this.getHours();
  176.                         return hours > 12 ? hours - 12 : hours;
  177.                     },
  178.                    
  179.                     //24-hour format of an hour without leading zeros
  180.                     G: function () {
  181.                         return this.getHours();
  182.                     },
  183.                    
  184.                     //12-hour format of an hour with leading zeros
  185.                     h: function () {
  186.                         var hours = pattern['g'].call(this);
  187.                         return hours < 10 ? '0' + hours : hours;
  188.                     },
  189.                    
  190.                     //24-hour format of an hour with leading zeros
  191.                     H: function () {
  192.                         var hours = pattern['G'].call(this);
  193.                         return hours < 10 ? '0' + hours : hours;
  194.                     },
  195.                    
  196.                     //Minutes with leading zeros
  197.                     i: function () {
  198.                         return this.getMinutes() < 10 ? '0' + this.getMinutes() : this.getMinutes();
  199.                     },
  200.                    
  201.                     //Seconds, with leading zeros
  202.                     s: function () {
  203.                         return this.getSeconds() < 10 ? '0' + this.getSeconds() : this.getSeconds();;
  204.                     },
  205.                    
  206.                     //Microseconds
  207.                     u: function () {
  208.                         return this.getMilliseconds();
  209.                     },
  210.                    
  211.                     //Timezone identifier
  212.                     e: function () {
  213.                         return null;
  214.                     },
  215.                    
  216.                     //Whether or not the date is in daylight saving time
  217.                     I: function () {
  218.                         return null;
  219.                     },
  220.                    
  221.                     //Difference to Greenwich time (GMT) in hours
  222.                     O: function () {
  223.                         var offset = this.getTimezoneOffset() / 60;
  224.                         return (offset < 0 ? '' : '+') + (offset < 10 ? '0' + offset : offset) + '00';
  225.                     },
  226.                    
  227.                     //Difference to Greenwich time (GMT) with colon between hours and minutes
  228.                     P: function () {
  229.                         var offset = pattern['O'].call(this);
  230.                         return offset.slice(0,3) + ':' + offset.slice(-2);
  231.                     },
  232.                    
  233.                     //Timezone abbreviation
  234.                     T: function () {
  235.                         return null;
  236.                     },
  237.                    
  238.                     //Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive
  239.                     Z: function () {
  240.                         return parseInt(pattern['O'].call(this)) * 60;
  241.                     },
  242.                    
  243.                     //ISO 8601 date
  244.                     c: function () {
  245.                         function pad(x) {
  246.                             return x < 10 ? '0'+ x : x;
  247.                         }
  248.                        
  249.                         return this.getUTCFullYear() + '-'
  250.                                 + pad(this.getUTCMonth() + 1) + '-'
  251.                                 + pad(this.getUTCDate()) + 'T'
  252.                                 + pad(this.getUTCHours()) + ':'
  253.                                 + pad(this.getUTCMinutes()) + ':'
  254.                                 + pad(this.getUTCSeconds()) + 'Z';
  255.                     },
  256.                    
  257.                     //RFC 2822 formatted date
  258.                     r: function () {
  259.                         return this.toUTCString();
  260.                     },
  261.                    
  262.                     //Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  263.                     U: function () {
  264.                         return this.getTime();
  265.                     }
  266.                 };
  267.  
  268.             return function(formatString) {
  269.                 var builder = '',
  270.                     character = '';
  271.                
  272.                 for (var i = 0; i <= formatString.length; i++) {
  273.                     character = formatString.charAt(i);
  274.                
  275.                     if(pattern.hasOwnProperty(character)) {
  276.                         builder += pattern[character].call(this).toString();
  277.                     }
  278.                     else {
  279.                         builder += character;
  280.                     }
  281.                 }
  282.  
  283.                 return builder;
  284.             }
  285.         }());
  286.    
  287.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement