Ext.util.Format.date,分钟与月份的误会

本文探讨了ExtJS中利用Ext.util.Format.date进行日期格式化时出现的一个常见误区:分钟与月份标识符混淆的问题。作者通过一个具体示例展示了如何避免这一错误,并正确地使用ExtJS内置的日期格式化功能。

Ext.util.Format.date,分钟与月份的误会

  3375人阅读  评论(0)  收藏  举报
  分类:
如下这段简单的代码:
[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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)里有问题,竟然用月份代替了分钟:
[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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,汗!
[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Ext.util.Format.date(d, 'Y-m-d H:i:s');  
也只有我这样的新手,才会犯这样的错误吧!Extjs的源码里有各种格式的功能的:
[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.   
  35.     c: function() {   
  36.         for (var c = "Y-m-dTH:i:sP", code = [], i = 0, l = c.length; i < l; ++i) {  
  37.             var e = c.charAt(i);  
  38.             code.push(e == "T" ? "'T'" : utilDate.getFormatCode(e));   
  39.         }  
  40.         return code.join(" + ");  
  41.     },          
  42.   
  43.     U: "Math.round(this.getTime() / 1000)"  
  44. },  

将分配仓库默认为第一条合同的送达仓库:Ext.define('erp.master.deliveryNoteManager.view.ContractAllocate', { extend: 'erp.ux.Panel', alias: 'widget.contract_allocate', iconCls: 'page_find', closable: true, listeners: { 'close': function (cmp) { var me = this; cmp.destroy(); } }, initComponent: function () { var me = this; var login_id = me.login_id; var ip = me.ip; me.ckStore = Ext.create('erp.master.deliveryNoteManager.store.QyWareHouse',{autoLoad:true}); Ext.apply(me.ckStore.proxy.extraParams, { shqybh: me.shqybh }); me.topStore = Ext.create('erp.master.deliveryNoteManager.store.ContractSumBot'); me.botStore = Ext.create('erp.master.deliveryNoteManager.store.ContractSumTop'); me.topStore.load({params: {login_id: login_id, ip: ip}}); me.botStore.load({params: {login_id: login_id, ip: ip}}); me.topStore.on({ //用于选择第一条记录 load: function (s, recs) { var grid = me.down('#grid_top'); erp.Util.gridSelect(grid, recs); } }); Ext.apply(me, { layout: {type: 'border', padding: 2}, tbar: [ {labelWidth:60,fieldLabel:erp.i18n.btn_fpck || '分配仓库', xtype : 'combo', store : me.ckStore, queryMode: 'remote', labelWidth:80, columnWidth: 0.5, itemId: 'ckbh', displayField : 'ckmc', valueField: 'ckbh' }, {text: erp.i18n.btn_btn_confirm || '确定', itemId: 'btn_confirm'}, {text: erp.i18n.btn_btn_updateAll || '批量修改', itemId: 'btn_update', hidden: true}, { text: erp.i18n.btn_btn_out || '退出', itemId: 'btn_exit', listeners: { click: function (field, e) { me.close(); } } } ], items: [{ region: 'center', flex: 1, split: true, itemId: 'grid_top', xtype: 'grid', overflowY: 'auto', overflowX: 'auto', selModel: Ext.create('Ext.selection.CheckboxModel'), listeners: { selectionchange: function (grid, recs) { if (recs.length > 0) { me.botStore.clearFilter(true); me.botStore.filter([Ext.create('Ext.util.Filter', { filterFn: function (item) { return item.get('clhh') == recs[0].get('clhh') && item.get('cltx1') == recs[0].get('cltx1'); }, root: 'data' })]); } } }, features: [{ ftype: 'summary', summaryType: 'count', dock: 'bottom' }], columns: [ { header: erp.i18n.bq_clhh || '材料货号', dataIndex: 'clhh', width: 70, summaryRenderer: function (value, summaryData, dataIndex) { return erp.i18n.qt_heji || '合计'; } }, {header: erp.i18n.bq_lbmc || '材料类别', dataIndex: 'lbmc', width: 80}, {header: erp.i18n.bq_clmc || '材料名称', dataIndex: 'clmc', width: 320}, {header: erp.i18n.bq_cltx1 || '规格尺寸', dataIndex: 'cltx1', width: 80}, {header: erp.i18n.bq_jldw || '单位', dataIndex: 'jldw', width: 50}, { header: erp.i18n.bq_dhsl || '到货数量', dataIndex: 'dhsl', width: 80, align: 'right', field: { xtype: 'numberfield', decimalPrecision: 4, selectOnFocus: true, minValue: 0 }, summaryType: 'sum', summaryRenderer: function (value, summaryData, dataIndex) { return Ext.util.Format.number(value, '0,000'); } }, {header: erp.i18n.bq_fzdw || '辅助单位', dataIndex: 'fzdw', width: 50}, { header: erp.i18n.bq_fzsl || '辅助数量', dataIndex: 'fzsl', width: 80, align: 'right', field: { xtype: 'numberfield', decimalPrecision: 4, selectOnFocus: true, minValue: 0 } }, {header: erp.i18n.bq_tmdhsl || '收货数量', dataIndex: 'tmdhsl', width: 80, align: 'right'}, {header: erp.i18n.bq_tmxs || '收货箱数', dataIndex: 'tmxs', width: 80, align: 'right'} ], store: me.topStore, plugins: Ext.create('Ext.grid.plugin.CellEditing', { ptype: 'cellediting', clicksToEdit: 1, autoCancel: false, listeners: { edit: function (field, e) { var rec = e.record; var botRecs = me.botStore.getRange(); switch (e.field) { case 'dhsl': var newDhsl = e.value; var wdsl = rec.get('wdsl'); var fzzbj = rec.get('fzzbj'); var zhxs = rec.get('zhxs'); if (fzzbj != 2 && fzzbj != 4 && fzzbj != 5 && fzzbj != 6) { if (zhxs != 0) { rec.set('fzsl', Ext.util.Format.round(newDhsl * zhxs, 3)); } else { rec.set('fzsl', 0); } } if (newDhsl > wdsl) { Ext.each(botRecs, function (r) { r.set('bcdh', Ext.util.Format.round(newDhsl * r.get('dhsl') / wdsl, 3)); if (fzzbj != 2 && fzzbj != 4 && fzzbj != 5 && fzzbj != 6) { if (zhxs != 0) { r.set('bcfzsl', Ext.util.Format.round(newDhsl * zhxs * r.get('dhsl') / wdsl, 3)) } else { r.set('bcfzsl', 0); } } }); } else { Ext.each(botRecs, function (r) { var bcdh = r.get('dhsl'); if (bcdh > newDhsl || newDhsl == 0) { r.set('bcdh', newDhsl); if (fzzbj != 2 && fzzbj != 4 && fzzbj != 5 && fzzbj != 6) { if (zhxs != 0) { r.set('bcfzsl', Ext.util.Format.round(newDhsl * zhxs, 3)) } else { r.set('bcfzsl', 0); } } } else { r.set('bcdh', bcdh); if (fzzbj != 2 && fzzbj != 4 && fzzbj != 5 && fzzbj != 6) { if (zhxs != 0) { r.set('bcfzsl', Ext.util.Format.round(bcdh * zhxs, 3)) } else { r.set('bcfzsl', 0); } } } newDhsl = Ext.util.Format.round(newDhsl - r.get('bcdh'), 3); }); } // me.botStore.sync(); break; case 'fzsl': if (rec.get('fzdw') == null || rec.get('fzdw').trim() == '') { break; } var newFzsl = e.value; var wdsl = rec.get('wdsl'); var fzzbj = rec.get('fzzbj'); var zhxs = rec.get('zhxs'); if (fzzbj != 2 && fzzbj != 4 && fzzbj != 5 && fzzbj != 6) { if (zhxs != 0) { rec.set('dhsl', Ext.util.Format.round(newFzsl / zhxs, 3)); } else { rec.set('fzsl', 0); } var dhzs = rec.get('dhsl'); if (dhzs > wdsl) { Ext.each(botRecs, function (r) { r.set('bcdh', Ext.util.Format.round(dhzs * r.get('dhsl') / wdsl, 3)); r.set('bcfzsl', Ext.util.Format.round(newFzsl * r.get('dhsl') / wdsl, 3)) }); } else { Ext.each(botRecs, function (r) { var bcdh = r.get('dhsl'); if (bcdh > dhzs || dhzs == 0) { r.set('bcdh', dhzs); r.set('bcfzsl', Ext.util.Format.round(dhzs * zhxs, 3)); } else { r.set('bcdh', bcdh); r.set('bcfzsl', Ext.util.Format.round(bcdh * zhxs, 3)); } dhzs = Ext.util.Format.round(dhzs - r.get('bcdh'), 3); }); } } else { var wdsl = rec.get('wdsl'); var wrfzsl = rec.get('wrfzsl'); if (newFzsl > wrfzsl) { Ext.each(botRecs, function (r) { r.set('bcfzsl', Ext.util.Format.round(newFzsl * r.get('dhsl') / wdsl, 3)); }); } else { Ext.each(botRecs, function (r) { var bcfzsl = r.get('fzsl'); if (bcfzsl > newFzsl || newFzsl == 0) { r.set('bcfzsl', newFzsl); } else { r.set('bcfzsl', bcfzsl); } newFzsl = Ext.util.Format.round(newFzsl - r.get('bcfzsl'), 3); }); } } // me.botStore.sync(); break; } } } }) }, { region: 'south', split: true, flex: 1, itemId: 'grid_bot', xtype: 'grid', overflowY: 'auto', overflowX: 'auto', selModel: Ext.create('Ext.selection.CheckboxModel'), features: [{ ftype: 'summary', summaryType: 'count', dock: 'bottom' }], columns: [ { header: erp.i18n.bq_hth || '合同号', dataIndex: 'hth', width: 70, summaryRenderer: function (value, summaryData, dataIndex) { return erp.i18n.qt_heji || '合计'; } }, {header: erp.i18n.bq_jhh || '计划号', dataIndex: 'jhh', width: 80}, { header: erp.i18n.bq_wkjq || '采计交期', dataIndex: 'wkjq', width: 80, align: 'center', xtype: 'datecolumn', format: 'Y-m-d' }, { header: erp.i18n.bq_jhrq || '交货日期', dataIndex: 'jhrq', width: 80, align: 'center', xtype: 'datecolumn', format: 'Y-m-d' }, { header: erp.i18n.bq_sxrq || '上线日期', dataIndex: 'sxrq', width: 80, align: 'center', xtype: 'datecolumn', format: 'Y-m-d' }, {header: erp.i18n.bq_clhh || '材料货号', dataIndex: 'clhh', width: 70}, {header: erp.i18n.bq_lbmc || '材料类别', dataIndex: 'lbmc', width: 90}, {header: erp.i18n.bq_clmc || '材料名称', dataIndex: 'clmc', width: 200}, {header: erp.i18n.bq_cltx1 || '规格尺寸', dataIndex: 'cltx1', width: 70}, {header: erp.i18n.bq_jldw || '单位', dataIndex: 'jldw', width: 50}, { header: erp.i18n.bq_bcdh || '到货数量', dataIndex: 'bcdh', width: 80, align: 'right', renderer: function (v) { if (v == 0) { return ' '; } else { return v; } }, summaryType: 'sum', summaryRenderer: function (value, summaryData, dataIndex) { return Ext.util.Format.round(value, 6); } }, {header: erp.i18n.bq_fzdw || '辅助单位', dataIndex: 'fzdw', width: 50}, { header: erp.i18n.bq_bcfzsl || '辅助数量', dataIndex: 'bcfzsl', width: 80, align: 'right', renderer: function (v) { if (v == 0) { return ' '; } else { return v; } } }, {header: erp.i18n.bq_ztdwmc || '主体单位', dataIndex: 'ztdwmc', width: 180} ], store: me.botStore }] }); me.callParent(arguments); } });
最新发布
12-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值