We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

Leech on 07/21/06


Tagged

class date format


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

jkochis


Date Format v1.0


Published in: JavaScript 


URL: http://jsfromhell.com/geral/date-format

Formats a date according to a formatting string. Created: 2006.05.05

  1. /*
  2. **************************************
  3. * Date.format Function v1.0 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7. Date.prototype.format = function(m, r){
  8. var d = this, a, fix = function(n, c){return (n = n + "").length < c ? new Array(++c - n.length).join("0") + n : n};
  9. var r = r || {}, f = {j: function(){return d.getDate()}, w: function(){return d.getDay()},
  10. y: function(){return (d.getFullYear() + "").slice(2)}, Y: function(){return d.getFullYear()},
  11. n: function(){return d.getMonth() + 1}, m: function(){return fix(f.n(), 2)},
  12. g: function(){return d.getHours() % 12 || 12}, G: function(){return d.getHours()},
  13. H: function(){return fix(d.getHours(), 2)}, h: function(){return fix(f.g(), 2)},
  14. d: function(){return fix(f.j(), 2)}, N: function(){return f.w() + 1},
  15. i: function(){return fix(d.getMinutes(), 2)}, s: function(){return fix(d.getSeconds(), 2)},
  16. ms: function(){return fix(d.getMilliseconds(), 3)}, a: function(){return d.getHours() > 11 ? "pm" : "am"},
  17. A: function(){return f.a().toUpperCase()}, O: function(){return d.getTimezoneOffset() / 60},
  18. z: function(){return (d - new Date(d.getFullYear() + "/1/1")) / 864e5 >> 0},
  19. L: function(){var y = f.Y(); return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},
  20. t: function(){var n; return (n = d.getMonth() + 1) == 2 ? 28 + f.L() : n & 1 && n < 8 || !(n & 1) && n > 7 ? 31 : 30},
  21. W: function(){
  22. var a = f.z(), b = 364 + f.L() - a, nd = (new Date(d.getFullYear() + "/1/1").getDay() || 7) - 1;
  23. return (b <= 2 && ((d.getDay() || 7) - 1) <= 2 - b) ? 1 :
  24. (a <= 2 && nd >= 4 && a >= (6 - nd)) ? new Date(d.getFullYear() - 1 + "/12/31").format("%W%") :
  25. (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
  26. }
  27. }
  28. return m.replace(/%(.*?)%/g, function(t, s){
  29. return r[s] ? r[s](d, function(s){return f[s] && f[s]();}) : f[s] ? f[s]() : "%" + (s && s + "%");
  30. });
  31. }

Report this snippet 

You need to login to post a comment.