排除数组中的值后,获取下一个最高日期值

时间:2013-11-18 12:11:19

标签: javascript jquery

我有myDate variable,其值为18-Nov-2013。每天都会更改其值.Tommorow此myDate variable的值为19-Nov-2013。我有一个列表我将mapped标记为array个名为exclude的{​​{1}},其中包含一些日期excluded,现在它的值为["20-Nov-2013",21-Nov-2013", "23-Nov-2010"]。我怎么可能从exclude array的值列表中过滤我的值。我需要数组中的next highest value。所以我需要在tommorrows date之后使用值22-Nov-2013。有人可以帮助我。

3 个答案:

答案 0 :(得分:2)

var excluded = ["30-Nov-2013","01-Dec-2013","02-Dec-2013"];
var myDate = "29-Nov-2013";
var month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var current = new Date(myDate);
while(true){
    current = new Date((current.getDate()+1<10? "0"+(current.getDate()+1):(current.getDate()+1))+ "-" + month[current.getMonth()] + "-" + current.getFullYear());
    var checkDate = (current.getDate()<10? "0"+(current.getDate()):(current.getDate()))+ "-" + month[current.getMonth()] + "-" + current.getFullYear();//this is necessary for when the +1 on day of month passes the month barrier
    if(-1 == excluded.indexOf(checkDate))
        break;

}
alert(checkDate);

答案 1 :(得分:1)

我不知道这是最好的方法,还是最好的算法,但你可以试试这个:

var myDate = ["17-Nov-2013", "18-Nov-2013"];
var excluded = ["20-Nov-2013", "21-Nov-2013", "23-Nov-2013"];
var months = {"Nov": 10}; // Add others months "Jan": 1, "Fev": 2 etc...

function findExcluded(date) 
{
    for (var i = 0; i < excluded.length; i++)
    {
        if (excluded[i] === date)
        {
            return true;
        }
    }

    return false;
}

function nextDate() 
{
    var last = myDate[(myDate.length - 1)];
    var s = last.split("-");
    var d = new Date(s[2], months[s[1]], s[0]);
    var next = new Date(d); 

    var chkDate = "";
    do 
    {
        next.setDate(next.getDate() + 1);
        chkDate = next.getDate() + "-" + findMonth(next.getMonth()) + "-" + next.getFullYear();
    } while(findExcluded(chkDate));

    return chkDate;
}

function findMonth(m) 
{
    var i = 10; // When you fill all months on 'months' array, this variable should start at '0' in order to loop to works.

    for (var month in months)
    {
        if (i == m)
        {
            return month;
        }
        i++;
    }
}

var nd = nextDate();
alert(nd);

请注意here

答案 2 :(得分:0)

没有代码?那么这将是我的方法:

1.Get next date for mydate. Say that is var nextDate.
2.Check whether that date exist in the array.
3.If exists add one more day to nextDate. Again check in the array. 
4.Do it until you get a date which is not present in your exclude array

要检查数组中是否存在,您可以使用arrValues.indexOf(nextDateInProperFormat) > -1