简体   繁体   English

for in loop无法按预期运行javascript

[英]for in loop not working as expected javascript

I have an object, Object { 2014-01-30=[1], 2014-02-01=[1]} 我有一个对象, Object { 2014-01-30=[1], 2014-02-01=[1]}

and an array called fechasPeriodo, 还有一个叫做fechasPeriodo的数组

so why is the following code: 那么为什么下面的代码是:

fechasPeriodo = [];
            for(var property in SelectedDates) {
            fechasPeriodo.push(new Date(property));
            }

producing this result [Date {Wed Jan 29 2014 18:00:00 GMT-0600}, Date {Fri Jan 31 2014 18:00:00 GMT-0600}] 产生此结果[Date {Wed Jan 29 2014 18:00:00 GMT-0600}, Date {Fri Jan 31 2014 18:00:00 GMT-0600}]

Edit: I'd expect the result to be Thu Jan 30 2014 etc. , Sun 02 Feb 2014. 编辑:我希望结果是2014年1月30日星期四等。2014年2月2日,星期日。

Actually it's a problem only because I'm trying to define a range of dates in datepicker using a Google Calendar feed. 实际上,这只是一个问题,因为我正尝试使用Google日历供稿在datepicker中定义日期范围。 So the following code: 所以下面的代码:

if(fechasPeriodo.length > 1) {
            r[1] = fechasPeriodo[0] <= date && date <= fechasPeriodo[1] ?"Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"):"Highlighted-unknown";
            }

I'd expect to highlight a range from the 30th Jan to the 02nd of February. 我希望重点介绍从1月30日到2月2日的范围。 But if you can guide me as to why it isn't working, I'd be very grateful, I'm following this fiddle: http://jsfiddle.net/qaEuj/ 但是,如果您能指导我为什么它不起作用,我将非常感激,我正在关注这个小提琴: http : //jsfiddle.net/qaEuj/

At the risk of being downvoted again I have to say that I still don't understand why my last bit of code above isn't working like the fiddle I mention, so here's my complete code: 冒着再次被投票的风险,我不得不说我仍然不明白为什么我上面的最后一部分代码不能像我提到的小提琴那样工作,所以这是我完整的代码:

    $(document).ready(function() {
    var fechaDefecto = new Date('2014/01/01');
    var fechaFin = new Date('2014/08/31');
    SelectedDates = null;

    /*SelectedDates[new Date('12/25/2014')] = new Date('12/25/2014');
    SelectedDates[new Date('12/12/2014')] = new Date('12/12/2014');
    SelectedDates[new Date('06/06/2014')] = new Date('06/06/2014');*/
    $('#tiposFechas').change(function() {
    $.getJSON("https://www.google.com/calendar/feeds/cide.edu_sm151i2pdhu2371vq8hamcver4@group.calendar.google.com/public/full?q="+encodeURI($(this).val()), {"alt" : "json"}, function(data) {
    SelectedDates = {};
    $.each(data.feed.entry, function(i, entry) {
    var key = entry.gd$when[0].startTime.substr(0, 10)
    var clave = entry.gd$when[0].endTime.substr(0, 10);
    if(key in SelectedDates === false || clave in SelectedDates === false) {
    SelectedDates[key] = [];
    SelectedDates[clave] = [];
    }
    SelectedDates[key].push(entry.title.$t);
    SelectedDates[clave].push(entry.title.$t);  
    });
    $('#cal').datepicker("refresh");
    });
    });

    $('#cal').datepicker(
    {
            beforeShowDay: function (date) {
            var r = [true, ""];
            if (SelectedDates === null) {
            r[1] = "Highlighted-unknown";
            }
                else {
                fechasPeriodo = [];
                for(var property in SelectedDates) {
                fechasPeriodo.push(new Date(property));
                //alert(property);
                }
                var key = $.datepicker.formatDate("yy-mm-dd", date);
                if(key in SelectedDates) {

                if(fechasPeriodo.length > 1) {
                r[1] = fechasPeriodo[0] <= date && date <= fechasPeriodo[1] ?"Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"):"Highlighted-unknown";
                }
                else {
                r[1] = "Highlighted"+SelectedDates[key][0].replace(/\s/g, "_"); 
                }
                r[2] = SelectedDates[key].join(", ");
                }
            }
            return r;
}, 
        minDate : fechaDefecto,
        maxDate : fechaFin, 
        numberOfMonths: [3,3]   
    });
});

I'm hoping someone points out a cause, even if it's a criticism, because it's getting late. 我希望有人指出一个原因,即使这是一个批评,因为它来晚了。

I must say I tried this: r[1] = new Date(fechasPeriodo[0].getYear(), fechasPeriodo[0].getMonth(), fechasPeriodo[0].getDate()) <= date && date <= new Date(fechasPeriodo[1].getYear(),fechasPeriodo[1].getMonth(),fechasPeriodo[1].getDate()) ?"Highlighted"+SelectedDates[key][0].replace(/\\s/g, "_"):"Highlighted-unknown"; 我必须说我尝试过: r[1] = new Date(fechasPeriodo[0].getYear(), fechasPeriodo[0].getMonth(), fechasPeriodo[0].getDate()) <= date && date <= new Date(fechasPeriodo[1].getYear(),fechasPeriodo[1].getMonth(),fechasPeriodo[1].getDate()) ?"Highlighted"+SelectedDates[key][0].replace(/\\s/g, "_"):"Highlighted-unknown";

But didn't work either. 但是也没有用。 May there be a problem in the way I'm representing the dates when I call new Date in the line fechasPeriodo.push(new Date(property)); 当我在fechasPeriodo.push(new Date(property));行中调用new Date时,表示日期的方式可能有问题fechasPeriodo.push(new Date(property)); and the way I'm comparing them? 以及我比较它们的方式?

It's just the difference between the UTC and non-UTC representation. 这只是UTC和非UTC表示形式之间的区别。

new Date('2014-01-30').toString(); //Wed Jan 29 2014 19:00:00
new Date('2014-01-30').toUTCString(); //Thu, 30 Jan 2014 00:00:00

Try fechasPeriodo[0].toUTCString(); 尝试fechasPeriodo[0].toUTCString(); and I'm pretty sure it will return what you expect. 而且我很确定它将返回您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM