简体   繁体   English

如何使用JavaScript在两个日期之间选择日期?

[英]How do I select dates between two dates with Javascript?

I have found similar threads about this but I cant seem to make their solutions work for my specific issue. 我发现了与此类似的话题,但是我似乎无法使他们的解决方案适用于我的特定问题。 I currently have a calendar that will highlight the starting date of an Event. 我目前有一个日历,可以突出显示活动的开始日期。 I need to change this to highlight the Start Date through the End Date. 我需要更改它以突出显示“开始日期”到“结束日期”。 Note: I did not write this code. 注意:我没有编写此代码。 It seems like whoever wrote this left a lot of junk in here. 似乎谁写的都在这里留下了很多垃圾。 Please don't judge. 请不要判断。

attachTocalendar : function(json, m, y) {
    var arr = new Array();
    if (json == undefined || !json.month || !json.year) {
        return;
    }
    m = json.month;
    y = json.year;

    if (json.events == null) {
        return;
    }
    if (json.total == 0) {
        return;
    }
    var edvs = {};
    var kds = new Array();
    var offset = en4.ynevent.getDateOffset(m, y);
    var tds = $$('.ynevent-cal-day');
    var selected = new Array(), numberOfEvent = new Array();
    for (var i = 0; i < json.total; ++i) {
        var evt = json.events[i];
        var s1 = evt.starttime.toTimestamp();
        var s0 = s1;
        var s2 = evt.endtime.toTimestamp();
        var ds = new Date(s1);
        var de = new Date(s2);
        var id = ds.getDateCellId();
        index = selected.indexOf(id);
        if (index < 0)
        {
            numberOfEvent[selected.length] = 1;
            selected.push(id);
        }
        else
        {
            numberOfEvent[index] = numberOfEvent[index] + 1;
        }
    }

    for (var i = 0; i < selected.length; i++) {
        var td = $(selected[i]);
        if (td != null) {
            if (!(td.hasClass("otherMonth"))){
                td.set('title', numberOfEvent[i] + ' ' + en4.core.language.translate(numberOfEvent[i] > 1 ? 'events' : 'event'));
                td.addClass('selected');
            }
        }
    }
},

Instead of trying to select them all, I recommend you iterate over them instead. 建议不要遍历它们,而建议您遍历它们。 So something like this: 所以像这样:

function highlightDates(startDate, endDate) {
    var curDate = startDate;
    var element;

    $('.selected').removeClass('selected'); // reset selection

    while (curDate <= endDate) {
        element = getElementForDate(curDate)
        element.addClass('selected');

        curDate.setDate(curDate.getDate() + 1); // add one day
    }
}

function getElementForDate(someDate) {
    return $('#day-' + someDate.getYear() + "-" + someDate.getMonth() + "-" + someDate.getDay());
}

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

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