使用 JavaScript 函数显示今天的约会/日期
Posted
技术标签:
【中文标题】使用 JavaScript 函数显示今天的约会/日期【英文标题】:show appointments/ dates of today with JavaScript function 【发布时间】:2016-03-24 19:12:36 【问题描述】:我想在我的 html 中显示当天的实际约会。我想用 javascript 来实现它,在其中我可以从 Excel 表中获取约会的名称和日期。 我已经写了一些东西,但它不起作用,而且不是我想要的方式。 代码:
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 表格中读取约会主题和日期?
【问题讨论】:
【参考方案1】:问题在于您的 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
【讨论】:
【参考方案2】:将您的代码更改为
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
【讨论】:
以上是关于使用 JavaScript 函数显示今天的约会/日期的主要内容,如果未能解决你的问题,请参考以下文章