2011-03-23 76 views
2

我有以下日期字段我想獲得一天前的日期,我怎麼能得到這個? 在我的表單面板如何獲得前一天的日期在extjs字段

items: [{ 
    fieldLabel: 'Start Date', 
    name: 'fromdate', 
    ref: '../fromdate', 
    id: 'fromdate', 
    vtype: 'daterange', 
    value: new Date(), 
    endDateField: 'todate' // id of the end date field 
}, { 
    fieldLabel: 'End Date', 
    name: 'todate', 
    id: 'todate', 
    vtype: 'daterange', 
    value: new Date(), 
    startDateField: 'fromdate' // id of the start date field 
}] 

今天,我收到2011年3月23日,但我想2011年3月22日我怎樣才能得到2011年3月22日?

回答

2

試試這個:

new Date().add(Date.DAY, -1); 
+0

這對我不起作用,因爲Date對象沒有添加方法。下面的答案使用Ext.Date.add確實有效 – 2012-10-07 10:12:54

0

或者這樣:

var date = new Date(); 
date.setDate(date.getDate()-1); 
6

說到ExtJS的語言,你可以使用Ext.Date庫操作的日期。例如:

1)在項目的價值配置:

 Ext.Date.add (new Date(),Ext.Date.DAY,1) 

根據您的代碼邏輯,則可以通過多種方式修改日期字段的默認值

 { 
      ... 
      vtype: 'daterange', 
      value : Ext.Date.add (new Date(),Ext.Date.DAY,1), 
      fromDateField: 'fromdate', 
      ... 
     } 

2)在組件的事件中。基本上,ExtJS容器有initComponent方法,它是最先運行的,但一定要在callParent()方法之後設置你的代碼才能訪問items集合。例如:

initComponent: function() { 
      ... 
    this.callParent(arguments); 
      this.items.getByKey('fromdate').value = Ext.Date.add (new Date(),Ext.Date.DAY,1); 
      ... 
} 
相關問題