更新了我自己的问题的答案。
我已用更强大的代码更新了自己的代码。
// JavaScript Document
var today = new Date;
var Event0 = new Date;
Event0.setDate(1);
Event0.setMonth(0); // January = 0
Event0.setFullYear(2011);
var Event1 = new Date;
Event1.setDate(1);
Event1.setMonth(1); // January = 0
Event1.setFullYear(2011);
var Event2 = new Date;
Event2.setDate(1);
Event2.setMonth(2); // January = 0
Event2.setFullYear(2011);
var Event3 = new Date;
Event3.setDate(1);
Event3.setMonth(3); // January = 0
Event3.setFullYear(2011);
var Event999 = new Date;
Event999.setDate(1);
Event999.setMonth(0); // January = 0
Event999.setFullYear(2012);
var ev = "<strong>Error\ !<\/strong><br\/>Could not find Event Feed"
var ev0 = "<strong>Error\ !<\/strong><br\/>Please check your computers date settings<br\/>Year set before 2011"
var ev1 = "Event 1"
var ev2 = "Event 2"
var ev3 = "Event 3"
var ev999 = "<strong>Error\ !<\/strong><br\/>Please check your computers date settings<br\/>Year set after 2012"
if (today <= Event0) var ev = ev0
if (today >= Event1) var ev = ev1
if (today >= Event2) var ev = ev2
if (today >= Event3) var ev = ev3
if (today >= Event999) var ev = ev999
- 旧问题 -
我正在尝试让以下代码设置输出。
--- ext JS文件中的代码---
<script type="text/javascript">
var today = new Date;
var Event1 = new Date;
Event1.setDate(1);
Event1.setMonth(0); // January = 0
Event1.setFullYear(2011);
var Event2 = new Date;
Event2.setDate(1);
Event2.setMonth(1); // January = 0
Event2.setFullYear(2011);
var Event3 = new Date;
Event3.setDate(1);
Event3.setMonth(2); // January = 0
Event3.setFullYear(2011);
var ev1 = "Event 1 at location A"
var ev2 = "Event 2 at location B"
var ev3 = "Event 3 at location C"
if (today >= Event1) var ev = ev1
if (today >= Event2) var ev = ev2
if (today >= Event3) var ev = ev3
</script>
---页面上的代码---
<script language="JavaScript"><!--
document.write(ev);
//-->
</script>
提前谢谢
答案 0 :(得分:0)
如果我理解正确,您正在尝试打印当月的名称。 试试这个:
<script type="text/javascript">
var Months =
[
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
];
var today = new Date;
</script>
<script language="JavaScript"><!--
document.write(Months[today.getMonth()]);
</script>
<强> P.S:强> 在您的代码中,您在if块中定义变量ev。请注意,ev将在if块范围内定义,因此在if块之外将不可用。 如果是块,则需要在块外定义它,如果是块则将其分配,如果是,则需要在外部访问。
答案 1 :(得分:0)
您的代码有效,但我不确定您的代码是否正在简单修改?
var today = new Date();
var Event1 = new Date(2011, 0, 1); //Jan 1, 2011
var Event2 = new Date(2011, 1, 1); //Feb 1, 2011
var Event3 = new Date(2011, 2, 1); //March 1, 2011
var ev = "No Event Found";
if(today >= Event3)
ev = "Event 3 at location C";
else if(today >= Event2)
ev = "Event 2 at location B";
else if(today >= Event1)
ev = "Event 1 at location A";
document.write(ev);
答案 2 :(得分:0)
您的代码有效,但这里有一些指示。当你的代码意图不明确时,很难给你更多的帮助。
var today = new Date();
// Easier way to set year, month, day
var events = [ new Date(2011, 0, 1), new Date(2011, 1, 1), new Date(2011, 2, 1)];
// Will contains all the events that are after today
var afterToday = [];
for (var i=0; i < events.length; i++) {
// Greater than works to compare dates because it calls the date's valueOf(),
// which calls getTime()
if (today > events[i]) {
afterToday.push(events[i]);
}
}
for (var i=0; i < afterToday.length; i++) {
alert("Today is before " + afterToday[i]);
}
// To check if there were any events after today
if (afterToday.length == 0) {
alert('No events after today');
}