Javascript代码无效

时间:2014-06-02 14:04:16

标签: javascript

我写了一个代码告诉我们什么是给出日期的日期,我得到了输入,但我无法获得输出,而且我能够处理它。 请帮助我,我用C ++编写了这个程序,但它运行良好,但我试图在浏览器中打开它,查看者可以在不下载任何内容的情况下访问我的代码。

<!DOCTYPE html>
<html>
<body>
<button onclick="date_day()">Try it</button>

<p id="demo"></p>

<script>
function date_day()
{
 var odd1=0,odd2=0,odd3=0,odd=0;
 document.write("EXAMPLE"+"<br>"+"************************************"+"<br>");
 document.write("Enter Year:2014"+"<br>"+"Enter Month:8"+"<br>"+"Enter date:20"+"        <br>"+"Wednesday"+"<br>"+"************************************"+"<br>");
 var date = prompt("Please enter Date (DD)","1");
 var month = prompt("Please enter Month (MM)","6");
  var year = prompt("Please enter Year (YYYY)","2014");
 /*odd_1_2()*/
 var cen,yr,i;
--year;
cen=(year/100)*100;
if((cen%400)==0)
    odd1=0;
else
{
    for(i=1;i<4;i++)
    {
        cen =cen-100;
        if(cen%400==0)
            break;

    } 
    switch(i*100)
{
    case 100: odd1 +=5;break;
    case 200: odd1 +=3;break;
    case 300: odd1 +=1;break;
}
}
yr=year%100;
odd2=(((yr/4)*2)+(yr-(yr/4)))%7;
/* oddthree*/
var nd=0,mon=1;
if(mon<month&&mon==1)
{
    nd +=31;
    mon++;
}
if(mon<month&&mon==2)
{
    if(year%4==0)
    {
        nd +=29;
        mon++;
    }
    else 
    {
        nd +=28;
        mon++;
    }

}
if(mon<month&&mon==3)
{
    nd +=31;
    mon++;
}
if(mon<month&&mon==4)
{
    nd +=30;
    mon++;
}
if(mon<month&&mon==5)
{
    nd +=31;
    mon++;
}
if(mon<month&&mon==6)
{
    nd +=30;
    mon++;
}
    if(mon<month&&mon==7)
    {
    nd +=31;
    mon++;
    }
    if(mon<month&&mon==8)
    {
    nd +=31;
    mon++;
    }
    if(mon<month&&mon==9)
    {
    nd +=30;
        mon++;
    }
    if(mon<month&&mon==10)
    {
        nd +=31;
        mon++;
    }
    if(mon<month&&mon==11)
    {
    nd +=30;
    mon++;
    }
    if(mon<month&&mon==12)
    {
        nd +=31;
        mon++;
    }
    nd +=dt;
    odd3=(nd%7);
    odd=(odd3)%7;
    switch(odd)
    {
        case 0:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Sunday");break;
        case 1:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Monday");break;
        case 2:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Tuesday");break;
        case 3:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Wednesday");break;
        case 4:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Thrusday");break;
        case 5:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is "+"Friday");break;
        case 6:  document.write("TheDay at "+date+"/"+month+"/"+year+"/"+" is             "+"Saturday");break;
    }
        }


</script>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

你可以通过使用名为.getDay();

的Date对象的内置函数来简化这一过程

检查here

答案 1 :(得分:0)

您正在使用document.write,这将重写页面的所有html。 如果要使用javascript向页面添加内容,可以使用element.innerHTML

假设您有<div id="myDiv"></div>,请执行编码document.getElementById('myDiv').innerHTML += data而不是document.write

答案 2 :(得分:0)

使用JavaScript的内置日期对象

var inp = prompt("Enter date");
var d = new Date(inp);
var dow = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getUTCDay()];
alert("That falls on a " + dow);