基于日期的JavaScript输出

时间:2011-02-16 16:57:00

标签: javascript

更新了我自己的问题的答案。

我已用更强大的代码更新了自己的代码。

// 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

- 旧问题 -

我正在尝试让以下代码设置输出。

  • 如果Event1日期为=或更高 然后今天的日期显示ev1。
  • 如果Event2日期为=或更高 然后今天的日期停止显示ev1 并显示ev2
  • 如果Event3日期为=或更高 然后今天的日期停止显示ev2 并显示ev3
问:有没有更好的方法可以做到这一点,或者我的代码可以产生我正在寻找的结果。我是JavaScript的新手,所以我希望我做得对。

--- 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>

提前谢谢

3 个答案:

答案 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');
}