Advertisement
Guest User

Date Format

a guest
Mar 30th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. Date.prototype.format = function ( format, r, locale ) {
  2. // by Giacomo Trudu
  3. r = ( typeof r != 'undefined' && r );
  4.  
  5. var time = this;
  6.  
  7. // Build the Date object
  8. var date = ( typeof time != 'undefined' ) ? ( time instanceof Date ? time : new Date( time * 1000 ) ) : new Date;
  9.  
  10. // Number of seconds since the beginning of this year
  11. var year_seconds = ( date - new Date( date.getFullYear(), 0, 1 ) ) / 1000;
  12.  
  13. // Retrieve the date informations
  14. var meta = String( date ).match( /^.*?([A-Z]{1,4})([\-+]\d{4}) \(([A-Z]+)\).*$/ );
  15.  
  16. date = {
  17.  
  18. d : date.getDate(),
  19. D : date.getDay(),
  20. m : date.getMonth(),
  21. y : date.getFullYear(),
  22. l : ( new Date( date.getFullYear(), 1, 29 ).getMonth() === 1 | 0 ),
  23. h : date.getHours(),
  24. M : date.getMinutes(),
  25. s : date.getSeconds(),
  26. u : date.getMilliseconds(),
  27. t : date.getTime(),
  28. z : date.getTimezoneOffset()
  29. };
  30.  
  31. // Stringa della data formattata
  32. var timestr = '';
  33.  
  34. // Riempie di zeri le cifre alla sinistra di un numero
  35. var pad = function ( value, len ) {
  36.  
  37. return ( '000000000' + String( value ) ).slice( -len );
  38. };
  39.  
  40. if (!locale) locale = 'it';
  41. var locale_string = {
  42. it: {
  43. D: [ 'Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab' ],
  44. l: [ 'Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato' ],
  45. S: [ '', '', '', '' ],
  46. F: [ 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic' ],
  47. M: [ 'gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre' ]
  48. },
  49. en: {
  50. D: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
  51. l: [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ],
  52. S: [ 'th', 'st', 'nd', 'rd' ],
  53. F: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
  54. M: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
  55. }
  56. }
  57.  
  58. // Define the format characters
  59. var fmt_characters = {
  60.  
  61. d : function () { return pad( date.d, 2 ); },
  62. D : function () { return locale_string[locale].D[ date.D ]; },
  63. j : function () { return date.d; },
  64. l : function () { return locale_string[locale].l[ date.D ]; },
  65. N : function () { return date.D + 1; },
  66. S : function () { return locale_string[locale].S[ date.d % 10 > 3 ? 0 : ( date.d < 10 || date.d > 20 ) * date.d % 10 ]; },
  67. w : function () { return date.D; },
  68. z : function () { return Math.ceil( year_seconds / 86400 ); },
  69. W : function () { return Math.ceil( year_seconds / 604800 ); },
  70. F : function () { return locale_string[locale].F[ date.m ]; },
  71. m : function () { return pad( date.m + 1, 2 ); },
  72. M : function () { return locale_string[locale].M[ date.m ]; },
  73. n : function () { return date.m + 1; },
  74. t : function () { return [31, (date.l ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][ date.m ]; },
  75. L : function () { return date.l; },
  76. Y : function () { return date.y; },
  77. y : function () { return String( date.y ).slice( -2 ); },
  78. a : function () { return ( date.h < 12 ? 'am' : 'pm' ); },
  79. A : function () { return ( date.h < 12 ? 'AM' : 'PM' ); },
  80. g : function () { return date.h % 12 || 12; },
  81. G : function () { return date.h; },
  82. h : function () { return pad( date.h % 12 || 12, 2 ); },
  83. H : function () { return pad( date.h, 2 ); },
  84. i : function () { return pad( date.M, 2 ); },
  85. s : function () { return pad( date.s, 2 ); },
  86. u : function () { return date.u * 1000; },
  87. I : function () { return ( date.m > 2 && date.m < 10 || ( date.m == 2 && date.D - date.d >= 8 - 1 ) ); },
  88. O : function () { return meta[2]; },
  89. P : function () { return meta[2].slice( 0, -2 ) + ':' + meta[2].slice( -2 ); },
  90. T : function () { return meta[3]; },
  91. Z : function () { return -date.z * 60; },
  92. c : function () { return ( !r ? formatDate( 'Y-m-d\\TH:i:sP', time, true ) : null ); },
  93. r : function () { return ( !r ? formatDate( 'D, d M Y H:i:s O', time, true ) : null ); },
  94. U : function () { return Math.floor( date.t / 1000 ); }
  95. };
  96.  
  97. // Split the format string into tokens
  98. var tokens = format.match( /(\\.|.)/gi );
  99.  
  100. // Make the time string
  101. for ( var i = 0; i < tokens.length; i++ )
  102. timestr += ( tokens[i] in fmt_characters ? fmt_characters[ tokens[i] ]() : ( tokens[i].length == 1 ? tokens[i] : tokens[i][1] ) );
  103.  
  104. return timestr;
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement