Ext.util.Format.date 时间格式的设置与转换

时间:2023-03-08 17:00:07

Ext.util.Format.date

如下这段简单的代码: 
  1. var d = new Date(value.time);
  2. var s = Ext.util.Format.date(d, 'Y-m-d H:m:s');
  3. return s;

在Chrome的Develop Tools中断点调试,发现这些变量的值为:
value.time: 1396257528000
d: Mon Mar 31 2014 17:18:48 GMT+0800 (中国标准时间)
s: "2014-03-31 17:03:48"
年月日时分秒中,只有分钟不对,硬生生的差了15分钟,汗!
单步跟踪进去,发现是Extjs的源码(VM13060)里有问题,竟然用月份代替了分钟:

  1. (function() {
  2. var Ext = window.ExtBox1;
  3. return Ext.String.leftPad(this.getFullYear(), 4, '0') + '-' + Ext.String.leftPad(this.getMonth() + 1, 2, '0') + '-'
  4. + Ext.String.leftPad(this.getDate(), 2, '0') + ' ' + Ext.String.leftPad(this.getHours(), 2, '0') + ':'
  5. + Ext.String.leftPad(this.getMonth() + 1, 2, '0') + ':' + Ext.String.leftPad(this.getSeconds(), 2, '0')
  6. })

仔细一想,发现是分钟(minute)与月份(month)搞错了,分钟应该用i而不是m

  1. Ext.util.Format.date(d, 'Y-m-d H:i:s');

Extjs的源码里有各种格式的功能的:

  1. formatCodes : {
  2. d: "Ext.String.leftPad(this.getDate(), 2, '0')",
  3. D: "Ext.Date.getShortDayName(this.getDay())",
  4. j: "this.getDate()",
  5. l: "Ext.Date.dayNames[this.getDay()]",
  6. N: "(this.getDay() ? this.getDay() : 7)",
  7. S: "Ext.Date.getSuffix(this)",
  8. w: "this.getDay()",
  9. z: "Ext.Date.getDayOfYear(this)",
  10. W: "Ext.String.leftPad(Ext.Date.getWeekOfYear(this), 2, '0')",
  11. F: "Ext.Date.monthNames[this.getMonth()]",
  12. m: "Ext.String.leftPad(this.getMonth() + 1, 2, '0')",
  13. M: "Ext.Date.getShortMonthName(this.getMonth())",
  14. n: "(this.getMonth() + 1)",
  15. t: "Ext.Date.getDaysInMonth(this)",
  16. L: "(Ext.Date.isLeapYear(this) ? 1 : 0)",
  17. o: "(this.getFullYear() + (Ext.Date.getWeekOfYear(this) == 1 && this.getMonth() > 0 ? +1 : "
  18. "(Ext.Date.getWeekOfYear(this) >= 52 && this.getMonth() < 11 ? -1 : 0)))",
  19. Y: "Ext.String.leftPad(this.getFullYear(), 4, '0')",
  20. y: "('' + this.getFullYear()).substring(2, 4)",
  21. a: "(this.getHours() < 12 ? 'am' : 'pm')",
  22. A: "(this.getHours() < 12 ? 'AM' : 'PM')",
  23. g: "((this.getHours() % 12) ? this.getHours() % 12 : 12)",
  24. G: "this.getHours()",
  25. h: "Ext.String.leftPad((this.getHours() % 12) ? this.getHours() % 12 : 12, 2, '0')",
  26. H: "Ext.String.leftPad(this.getHours(), 2, '0')",
  27. i: "Ext.String.leftPad(this.getMinutes(), 2, '0')",
  28. s: "Ext.String.leftPad(this.getSeconds(), 2, '0')",
  29. u: "Ext.String.leftPad(this.getMilliseconds(), 3, '0')",
  30. O: "Ext.Date.getGMTOffset(this)",
  31. P: "Ext.Date.getGMTOffset(this, true)",
  32. T: "Ext.Date.getTimezone(this)",
  33. Z: "(this.getTimezoneOffset() * -60)",
  34. c: function() {
  35. for (var c = "Y-m-dTH:i:sP", code = [], i = 0, l = c.length; i < l; ++i) {
  36. var e = c.charAt(i);
  37. code.push(e == "T" ? "'T'" : utilDate.getFormatCode(e));
  38. }
  39. return code.join(" + ");
  40. },
  41. U: "Math.round(this.getTime() / 1000)"
  42. },

原文链接:https://blog.****.net/gaojinshan/article/details/22683777