jquery.dateformat.min.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*!
  2. Date formatting plugin
  3. Copyright (c) Eric Garside
  4. Dual licensed under:
  5. MIT: http://www.opensource.org/licenses/mit-license.php
  6. GPLv3: http://www.opensource.org/licenses/gpl-3.0.html
  7. */"use strict";(function($){var months=['January','February','March','April','May','June','July','August','September','October','November','December'],days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],counts=[31,28,31,30,31,30,31,31,30,31,30,31],suffix=[null,'st','nd','rd'],_;function pad(format,string,length)
  8. {format=format+'';length=length||2;return format.length<length?new Array(1+length-format.length).join(string)+format:format;}
  9. function rpad(format,string,length)
  10. {format=format+'';length=length||2;return format.length<length?format+new Array(1+length-format.length).join(string):format;}
  11. function modCalc(date,mod1,mod2)
  12. {return(Math.floor(Math.floor(date.valueOf()/1e3)/mod1)%mod2);}
  13. function formatDate(date,format)
  14. {format=format.split('');var output='',buffering=false,char='',index=0;for(;index<format.length;index++)
  15. {char=format[index]+'';switch(char)
  16. {case' ':output+=char;break;case'{':case'}':buffering=char==='{';break;default:if(!buffering&&_[char])
  17. {output+=_[char].apply(date);}
  18. else
  19. {output+=char;}
  20. break;}}
  21. return output;}
  22. _={V:function()
  23. {return modCalc(this,864e2,1e5);},v:function()
  24. {return pad(_.V.apply(this),0);},K:function()
  25. {return _.V.apply(this)%365;},k:function()
  26. {return pad(_.K.apply(this),0);},X:function()
  27. {return modCalc(this,36e2,24);},x:function()
  28. {return pad(_.X.apply(this),0);},p:function()
  29. {return modCalc(this,60,60);},C:function()
  30. {return pad(_.p.apply(this),0);},E:function()
  31. {return(_.X.apply(this)*60)+_.p.apply(this);},e:function()
  32. {return pad(_.e.apply(this),0);},d:function()
  33. {return pad(this.getDate(),0);},D:function()
  34. {return days[this.getDay()].substring(0,3);},j:function()
  35. {return this.getDate();},l:function()
  36. {return days[this.getDay()];},N:function()
  37. {return this.getDay()+1;},S:function()
  38. {return suffix[this.getDate()]||'th';},w:function()
  39. {return this.getDay();},z:function()
  40. {return Math.round((this-_.f.apply(this))/864e5);},W:function()
  41. {return Math.ceil(((((this-_.f.apply(this))/864e5)+_.w.apply(_.f.apply(this)))/7));},F:function()
  42. {return months[this.getMonth()];},m:function()
  43. {return pad((this.getMonth()+1),0);},M:function()
  44. {return months[this.getMonth()].substring(0,3);},n:function()
  45. {return this.getMonth()+1;},t:function()
  46. {if(this.getMonth()===1&&_.L.apply(this)===1)
  47. {return 29;}
  48. return counts[this.getMonth()];},L:function()
  49. {var Y=_.Y.apply(this);return Y%4?0:Y%100?1:Y%400?0:1;},f:function()
  50. {return new Date(this.getFullYear(),0,1);},Y:function()
  51. {return this.getFullYear();},y:function()
  52. {return(''+this.getFullYear()).substr(2);},a:function()
  53. {return this.getHours()<12?'am':'pm';},A:function()
  54. {return _.a.apply(this).toUpperCase();},B:function()
  55. {return pad(Math.floor((((this.getHours())*36e5)+(this.getMinutes()*6e4)+(this.getSeconds()*1e3))/864e2),0,3);},g:function()
  56. {return this.getHours()%12||12;},G:function()
  57. {return this.getHours();},h:function()
  58. {return pad(_.g.apply(this),0);},H:function()
  59. {return pad(this.getHours(),0);},i:function()
  60. {return pad(this.getMinutes(),0);},s:function()
  61. {return pad(this.getSeconds(),0);},u:function()
  62. {return this.getTime()%1e3;},O:function()
  63. {var t=this.getTimezoneOffset()/60;return rpad(pad((t>=0?'+':'-')+Math.abs(t),0),0,4);},P:function()
  64. {var t=_.O.apply(this);return t.subst(0,3)+':'+t.substr(3);},Z:function()
  65. {return this.getTimezoneOffset()*60;},c:function()
  66. {return _.Y.apply(this)+'-'+_.m.apply(this)+'-'+_.d.apply(this)+'T'+_.H.apply(this)+':'+_.i.apply(this)+':'+_.s.apply(this)+_.P.apply(this);},r:function()
  67. {return this.toString();},U:function()
  68. {return this.getTime()/1e3;}};$.extend(Date.prototype,{format:function(format)
  69. {return formatDate(this,format);}});$.dateformat={rules:function(custom)
  70. {if(custom!==undefined)
  71. {_=$.extend(_,custom);}
  72. return _;},hasRule:function(rule)
  73. {return _[rule]!==undefined;},get:function(type,date)
  74. {return _[type].apply(date||new Date());},format:function(format,date)
  75. {return formatDate(date||new Date(),format);},pad:pad,rpad:rpad};}(jQuery));