JavaScript - 在特定日期显示内容

时间:2014-04-28 19:12:14

标签: javascript date if-statement switch-statement

我有这个switch语句,它根据星期几在我的主页上显示内容。在案例6(星期六),我只需要证明每隔一个星期六就有一个陪练班。我觉得我需要在switch语句的情况6中使用if语句,但我不知道如何编写它(绝对的初学者)。

任何人都可以帮助我吗?我的代码如下。谢谢!

仅限案例6(以下完整代码)-------------------------------------- ------------------> 我希望只在这些日期显示这些课程,而不是让用户对可用日期进行筛选。  案例6://星期六         x =" 10:30-12:00pm |女子拳击&#34 ;;          +"
" +          " 1:00-3:00pm |陪练班"          +"
" +"
" +          " 2014年夏季|仅限6次会议"          +"
" +          " 陪练和调节"          +"
" +          "在下列日期的12:30-2:00:6月7日& 7月12日21日& 8月9日,26日& 23"          +"
" +          "在前往此处之前检查您的日历!&#34 ;;         打破;

完整代码--------------------------------------------- ---------------------------------->

//Display today's date above today's classes
var todaysDate = (function(){
var d=new Date();

    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";

    var month=new Array(11);
    month[0]="January";
    month[1]="February";
    month[2]="March";
    month[3]="April";
    month[4]="May";
    month[5]="June";
    month[6]="July";
    month[7]="August";
    month[8]="September";
    month[9]="October";
    month[10]="November";
    month[11]="December";

    var currentTime = new Date()
    var dayOfWeek = weekday[d.getDay()];
    var month = month[d.getMonth()];
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()

    document.write(dayOfWeek + ", " + month + " " + day + ", " + year);
});

//Switch statement to display 'today's classes' based on the day of the week
function todaysClasses(){
var x;
var d=new Date().getDay();
    switch (d)  {

      case 0:
        x="<strong>" + "The gym is closed on Sundays." + "</strong>";
        break;

      case 1://Monday
        x="4:30-5:15pm | Total Body"
         + "<br />" +
         "5:30-6:00pm | Core"
         + "<br />" +
         "6:00-6:30pm | Heavy Bag Class"
         + "<br />" +
         "6:00-7:00pm | Free Trial Class"
         + "<br />" +
         "6:30-7:00pm | Strength"
         + "<br />" +
         "7:00-8:30pm | Coed Boxing";
        break;

      case 2://Tuesday
        x="4:00-4:45pm | Total Body"
         + "<br />" +
         "5:00-5:30pm | Core"
         + "<br />" +
         "5:30-6:00pm | Strength"
         + "<br />" +
         "6:00-6:30pm | Heavy Bag Class"
         + "<br />" +
         "6:30-7:00pm | Cardio"
         + "<br />" +
         "7:00-8:30pm | Coed Boxing";
        break;

      case 3://Wednesday
        x="4:30-5:15pm | Total Body"
         + "<br />" +
         "5:30-6:00pm | Core"
         + "<br />" +
         "6:00-6:30pm | Strength"
         + "<br />" +
         "6:30-7:00pm | Heavy Bag Class"
         + "<br />" +
         "6:30-8:00pm | Women's Boxing";
        break;

      case 4://Thursday
        x="4:30-5:00pm | Strength"
         + "<br />" +
         "5:00-5:30pm | Cardio"
         + "<br />" +
         "5:30-6:00pm | Heavy Bag Class"
         + "<br />" +
         "6:30-8:00pm | Coed Boxing";
        break;

      case 5://Friday
        x="There are no classes on Fridays.";
        break;

      case 6://Saturday
        x="10:30-12:00pm | Women's Boxing";
         + "<br />" +
         "1:00-3:00pm | Sparring Class"
         + "<br />" + "<br />" +
         "<b>Summer 2014 | 6 Sessions Only</b>"
         + "<br />" +
         "<b>Sparring and Conditioning</b>"
         + "<br />" +
         "12:30-2:00pm on the following dates: June 7 &amp; 21, July 12 &amp; 26, August 9 &amp; 23"
         + "<br />" +
         "Check your calendar before heading here!";
        break;
      }
document.getElementById("byday").innerHTML=x;
}

1 个答案:

答案 0 :(得分:0)

不太清楚......但我试过了。

你应该看一个jquery来操纵你的dom并且可能有一个后端来询问数据......但是目前,我建议你学习编写简单的函数,而不是200行的函数。

var datas =  [];

//This should be asked to something like a backend no?
function getDatas(){
//Sorry, I' lazy, complete yourself
    var datas =  
     [ {info : "<strong>Gym is closed on Sundays</strong>"}]

     ,[ {from:"4:30", to : "5:15", info:"TotalBody" },
       {from:"5:30", to : "6:15", info:"Core" }
     ];
    return datas;
};



function displayFor(date){
    var allDatas = getDatas();
    var dayDatas = allDatas[date.getDay()];

    //Just construct an html, it's not beautifull at all but simplier that yours.
    var displayText = "";
    for(var i=0;i<dayDatas.length;i++){
        var event = dayDatas[i];
        if(event.from && event.to ){
            displayText += (event.from + "-" + event.to );
        }
        displayText += event.info;
        displayText +="<br/>";
    };

    document.getElementById("byday").innerHTML= displayText;
}

对于如何在JS&#39;中显示格式化日期,只需google it:p