获取ExtJS中开始/结束日期之间的日期

时间:2015-03-06 14:00:11

标签: javascript date extjs

我想要获得两个日期之间的范围。这是我使用的代码:

          var startDateStr = Ext.getCmp('startDateFoi').getSubmitValue();
           //console.log(startDateStr); // 10-Mar-2015
          var endDateStr = Ext.getCmp('endDateFoi').getSubmitValue();
           //console.log(endDateStr); // 12-Mar-2015

            allDates = [];

            while (startDateStr <= endDateStr) {
                allDates.push(new Date(startDateStr));
                startDateStr.setDate(startDateStr.getDate() + 1);
            }

            console.log(allDates);

我在这里缺少什么?格式是否必须是某种特定类型?

2 个答案:

答案 0 :(得分:1)

您可以使用Ext.Date类来处理:Ext.Date.diff

// calculate the difference in days between two dates
var diff = Ext.Date.diff(date1, date2, 'd'));

或者,如果你想要标准的JS版本,那么你就知道它是如何工作的:

Fiddle Link

Ext.application({
    name: 'Fiddle',

    launch: function() {
        var processRequest = function() {
                var date1 = Ext.getCmp('date1').getValue();
                var date2 = Ext.getCmp('date2').getValue();

                // JS counts it in milliseconds
                var diff = (date2 - date1) / 1000;

                // Number of seconds in one day
                console.log(diff / 86400);
            };

        Ext.create('Ext.panel.Panel', {
            title: 'Choose a future date:',
            width: 300,
            bodyPadding: 10,
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'datepicker',
                id: 'date1',
                minDate: new Date(),
                handler: function(picker, date) {
                    processRequest();
                }
            }, {
                xtype: 'datepicker',
                id: 'date2',
                minDate: new Date(),
                handler: function(picker, date) {
                    processRequest();
                }
            }]
        });
    }
});

答案 1 :(得分:0)

毕竟我是这样做的: 首先我做了一个功能:

         var ImagedbyDate = function(){ 
           $(document).ready(function(){

        // GET ALL THE DATES BETWEEN TWO SELECTED DAYS AND STORE THEM IN AN ARRAY (allDates[])

             var startDateStr = Ext.getCmp('startDateFoi').getSubmitValue();
             alert(startDateStr);
             var endDateStr = Ext.getCmp('endDateFoi').getSubmitValue();
             alert(endDateStr);

             currentDate = new Date(startDateStr);
             endDate = new Date(endDateStr);
             alert(currentDate);
             allDates = [];

            while (currentDate <= endDate) {
                allDates.push(new Date(currentDate));
                currentDate.setDate(currentDate.getDate() + 1);
            }

实际上我添加了这两行:

        currentDate = new Date(startDateStr);
        endDate = new Date(endDateStr);

这是我的项目的外观:

    {
            region: 'center', 
            title: "Rural Broadband",
            layout: 'fit',
            collapsible: false,
            items: [mappanel] , //mapPanel
            dockedItems: [{ //Toolbar with Actions - Beginn
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                text: 'Current center of the map',
                handler: function(){
                    var c = GeoExt.panel.Map.guess().map.getCenter();
                    Ext.Msg.alert(this.getText(), c.toString());
                    }
                },{
                text: 'See Unassigned Images',
                handler: function(){
                    UnassignedImg();
                    }   
                },
                {
                    fieldLabel: 'Start Date',
                    name: 'startDate',
                    xtype: 'datefield',
                    id: 'startDateFoi',
                    format: 'd-M-Y'
                },
                {
                    fieldLabel: 'End Date',
                    name: 'startDate',
                    id: 'endDateFoi',
                    xtype: 'datefield',
                    format: 'd-M-Y'
                }, ...

我希望这有助于某人。