【问题标题】:Extjs 4.1 timefield returning 2008 as the yearExtjs 4.1 时域返回 2008 作为年份
【发布时间】:2016-04-03 21:49:16
【问题描述】:

我使用 extjs timefield 创建了一个简单的应用程序。这是代码&fiddle

Ext.create('Ext.form.Panel', {
    title: 'Time Card',
    width: 300,
    bodyPadding: 10,
    renderTo: Ext.getBody(),
    items: [{
        xtype: 'timefield',
        id: 'in',
        fieldLabel: 'Time In',
        //minValue: '6:00 AM',
        //maxValue: '8:00 PM',
        format: 'H:i',
        increment: 30,
        anchor: '100%'
    }, {
        xtype: 'button',
        text: 'click',
        handler: function(){
           alert(Ext.getCmp('in').getValue());
           console.log(Ext.getCmp('in').getValue());
        }
   }]
});

问题/问题:

  1. 当我使用 getValue() 时,它返回 Tue Jan 01 2008 01:00:00 GMT-0500 (Eastern Standard Time)我不知道为什么它会返回 2008 年?
  2. 有没有办法捕获时区信息?

【问题讨论】:

  • 看起来有问题。您应该自己设置日期部分。
  • Timefield 以及 datefield 实际值是日期对象,正如 Alexandex 在他的回答中提到的,它的初始日期设置为 2008 年 1 月 1 日。如果您想获得时间,可以使用Ext.Date.format(Ext.getCmp('in').getValue(), 'H:i') 或设置submitFormat: 'H:i' 并使用Ext.getCmp('in').getSubmitValue() 或仅使用Ext.getCmp('in').getRawValue()

标签: extjs extjs4 extjs4.1 extjs5 timefield


【解决方案1】:

timefield的来源包含

/**
 * @private
 * This is the date to use when generating time values in the absence of either minValue
 * or maxValue.  Using the current date causes DST issues on DST boundary dates, so this is an
 * arbitrary "safe" date that can be any date aside from DST boundary dates.
 */
initDate: '1/1/2008',
initDateParts: [2008, 0, 1],

您可以覆盖这些属性,尽管它们被标记为私有,但我不建议这样做。我建议使用日期字段和时间字段,然后根据时间计算日期。

JavaScript 不支持时区,它们只支持 UTC 和 Local。 ExtJS 也不支持时区。如果您需要任意时区,可以查看moment.js

【讨论】:

    【解决方案2】:

    您可以通过执行类似的操作在renderer 函数中获取正确的日期:

    renderTimeField: function(time) {
        var ndate = new Date(),
            tmin = time.getMinutes(),
            thour = time.getHours();
    
        return new Date(ndate.setHours(thour, tmin, 0, 0));
    },
    

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多