date.format.js 1.2 KB

1234567891011121314151617181920212223242526
  1. Date.prototype.format = function (f) {
  2. if (!this.valueOf()) return " ";
  3. var weekName = ["일요일", "월요일", "화요일", "수요일", "목요일", "금요일", "토요일"];
  4. var d = this;
  5. return f.replace(/(yyyy|yy|MM|dd|E|hh|mm|ss|a\/p)/gi, function ($1) {
  6. switch ($1) {
  7. case "yyyy": return d.getFullYear();
  8. case "yy": return (d.getFullYear() % 1000).zf(2);
  9. case "MM": return (d.getMonth() + 1).zf(2);
  10. case "dd": return d.getDate().zf(2);
  11. case "E": return weekName[d.getDay()];
  12. case "HH": return d.getHours().zf(2);
  13. case "hh": return ((h = d.getHours() % 12) ? h : 12).zf(2);
  14. case "mm": return d.getMinutes().zf(2);
  15. case "ss": return d.getSeconds().zf(2);
  16. case "a/p": return d.getHours() < 12 ? "오전" : "오후";
  17. default: return $1;
  18. }
  19. });
  20. };
  21. String.prototype.string = function (len) { var s = '', i = 0; while (i++ < len) { s += this; } return s; };
  22. String.prototype.zf = function (len) { return "0".string(len - this.length) + this; };
  23. Number.prototype.zf = function (len) { return this.toString().zf(len); };