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

Get the dates between start/end date in ExtJS

本文关键字:日期 结束 之间 开始 ExtJS 获取      更新时间:2023-09-26

我正在尝试获取两个日期之间的范围。这是我使用的代码:

          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);

我在这里错过了什么?格式必须是特定类型的吗?

您可以使用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();
                }
            }]
        });
    }
});

我就是这样做的:首先我做了一个函数:

         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'
                }, ...

我希望这能帮助到别人。