Jquery时间对象的格式化

来源:互联网 发布:it公司 知乎 编辑:程序博客网 时间:2024/05/17 22:54
  1. /**
  2. * 时间对象的格式化;
  3. */
  4. Date.prototype.format = function(format) {
  5. /*
  6. * eg:format="yyyy-MM-dd hh:mm:ss";
  7. */
  8. var o = {
  9. "M+" : this.getMonth() + 1,// month
  10. "d+" : this.getDate(),// day
  11. "h+" : this.getHours(),// hour
  12. "m+" : this.getMinutes(),// minute
  13. "s+" : this.getSeconds(),// second
  14. "q+" : Math.floor((this.getMonth() + 3) / 3),// quarter
  15. "S" : this.getMilliseconds()
  16. // millisecond
  17. }
  18. if (/(y+)/.test(format)) {
  19. format = format.replace(RegExp.$1, (this.getFullYear() +"").substr(4
  20. - RegExp.$1.length));
  21. }
  22. for (var kin o) {
  23. if (new RegExp("(" + k +")").test(format)) {
  24. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  25. ? o[k]
  26. : ("00" + o[k]).substr(("" + o[k]).length));
  27. }
  28. }
  29. return format;
  30. }