使用 JavaScript 功能显示今天的约会/日期

show appointments/ dates of today with JavaScript function

我想在 HTML 中显示当天的实际约会。我想用 JavaScript 来实现它,我从 excel table 中获取约会的名称和日期。 我已经写了一些东西,但它不起作用,而且不是我想要的方式。 代码:

function appointment() {

    var event = [];
    var temp = [];

    event[0] = ["17.12.2015", "test1"];
    event[1] = ["11.12.2015", "TestToday"];

    var datum = new Date();     
    var today = today.getDate();    
    var month = today.getMonth() + 1; 

    for (i = 0; i < event.length; i++) {
        if (event[i]) {
            if (event[i][0] == today && event[i][0] == month) {
                event[i][0] = temp[i];
            }
        }
        else {
            break;
        }
    }

    if (temp.length == 0) {
        document.write("Today is nothing to do");
    }
    else {
        var x2 = "Today " + ((temp.length == 1) ? "is following event: " : "are following events: ");
        for (i = 0; i < temp.length; i++) {
            x2 += ((temp[i] > 0) ? ((temp[i] == (temp.length - 1)) ? " and " : ", ") : " ") + temp[event[1]][3] + "(" + temp[event[i]][2] + ")";
        }
        document.write(x2 + " appointment");
    }

}

问题:我怎样才能让它工作?如何从 excel table 中读取约会主题和日期?

问题出在您的 for 循环中。比较今天的日期应该采用 dd.mm.yy 格式,因为它在数组中:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10)
    dd='0'+dd;

if(mm<10)
    mm='0'+mm;

var today = dd+'.'+mm+'.'+yyyy;

for (i = 0; i < event.length; i++) {
    if (event[i]) {
        if (event[i][0] == today) {
            temp[i] = event[i][0];
        }
    }
    // Don't break your for loop

}

Working Fiddle

将您的代码更改为

var datum = new Date().setHours(0,0,0,0);
//you don't need today and month
if (event[i]) {
   eventDate = new Date(event[i][0].split(".").reverse()).getTime();
   if (datum === eventDate) {
        temp.push(event[i][1]); //there was a problem here as well
   }
}
else {
   break;
}

jsFiddle