Javascript日期差异变量NaN

时间:2018-04-19 17:45:39

标签: javascript



<!DOCTYPE html>

<html>
<head>
<title></title>
<meta name="author" content="" />
<meta name="keywords" content="" />

<style type="text/css">

body {
background-color: rgb(250, 100, 100); }

#pc {
width: 500px;
height:300px;
background-color: rgb(247, 240, 224);
border: rgb(50, 153, 254) 5px groove;
margin-left: auto;
margin-right: auto;
margin-top: 100px; }

#clock {
background-color: rgb(50, 153, 254);
float: left;
border: rgb(50, 153, 254) 5px groove;
height: 150px;
width: 150px;
margin-top: 45px;
margin-left: 25px; }

#HT1 {
background-color: black;
color: yellow;
text-align: center;
margin-top: -1px;
margin-left: auto;
margin-right: auto;
width: 100px;
}

#HT2 {
background-color: black;
color: yellow;
text-align: center;
margin-top: -1px;
width: 125px;
margin-left: auto;
margin-right: auto;
}

#Date {
background-color: black;
color: yellow;
font-size: 18px;
text-align: center;
width: 125px;
margin-top: -12px;
margin-left: 15px;
}

#Time {
background-color: black;
color: yellow;
font-size: 18px;
text-align: center;
width: 125px;
margin-top: 50px;
margin-left: 15px;
}

#hourglass {
position: absolute;
margin-left: 43px;
}

#countdown {
background-color: rgb(50, 153, 254);
float: right;
border: rgb(50, 153, 254) 5px groove;
height: 200px;
width: 250px;
margin-top: 25px;
margin-right: 25px; }

#title {
text-align: center; 
margin-bottom: 0px; 
font-variant: small-caps;
color: yellow; 
background-color: black; 
width: 100%; 
margin-top: 0px;
border-bottom: rgb(50, 153, 254) 5px groove; 
height: 45px;
}       

#day {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#hours {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#minutes {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#seconds {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}
</style>

<script type="text/JavaScript" src="main.js">
</script>
</head>

<body onload="day(); current(); DateDiff();" >
<div id="pc">

<h1 id="title">Count Down to <span id="Event">_ _ _ _ _ _ _ _</span></h1>

<div id="clock">
<h3 id="HT1">Time</h3>

<div id="Date">
<span id=Month></span>/<span id="Day"></span>/<span id="Year"></span>
</div>

<img id="hourglass" src="clock.png" height="64px" width="64px" alt="This is an hourglass" />
<span id="Date" ></span>

<div id="Time">
<span id="Hours"></span>
</div>

</div>

<div id="countdown">
    <h3 id="HT2">Countdown</h3>
    <div id="day"><span id="ED">____</span> Days</div><br />
    <div id="hours"><span id="EH">____</span> Hours</div><br />
    <div id="minutes"><span id="EM">____</span> Minutes</div><br />
    <div id="seconds"><span id="ES">____</span> Seconds</div><br />
</div>



</div>
</body>

</html>
&#13;
&#13;
&#13;

在这个项目中,我试图弄清楚两个日期之间的差异有多长。一个日期是当前日期,另一个日期是人们选择的日期。我遇到的问题是因为变量中的日期是字符串而不是数字我得到NaN。我尝试使用parseInt()属性但是,这给了我8,这不是现在和除夕之间的差异。那么,如何将变量更改为数字,但保留日期格式?

这是我目前的代码,既可以找到未来的日期,也可以找到差异。

&#13;
&#13;
/* Future Date Variables */
var DayF;
var MonthF;
var FullF;
var event;
var time;
var someday;

/* Asks for event name and the date */
function day()
{
    event = prompt("Please input a day you want to count down too");
    while (event == 0 || event == null)
    {
        if (event == 0 || event == null)
        {
            alert("That is not a valid event, please try again");
            event = prompt("Please input a day you want to count down too");
        }
    }
    document.getElementById("Event").innerHTML = event;
    
    MonthF = prompt("Please input the month the event is in");
    DayF = prompt("Please input the day the event is on");
    FullF = prompt("Please input the year, note we do not go into the past");
    
    if(MonthF < 1 || Month > 12)
    	{
    		alert("There are only 12 months, please try again");
    		MonthF = prompt("Please input the month the event is in");
    	}
    	
    if(DayF > 31 || DayF < 1)
    	{
    		alert("There is only an average of 31 days in a month");
    		DayF = prompt("Please input the day the event is on");
    	}
    	
    if(FullF < 2018)
    	{
    		alert("I said we do not go into the past");
    		FullF = prompt("Please input the year, note we do not go into the past");
    	}
    someday =(MonthF + "/" + DayF + "/" + FullF);
}

/* Current Time and Date Variables */
var nd = new Date();
var M = nd.getMonth();
var D = nd.getDate();
var F = nd.getFullYear();
var H = nd.getHours();
var Min = nd.getMinutes();
var Sec = nd.getSeconds();
var ap = "am";

/* Gets and Displays current time */
function current()
{
	M = M + 1;

	document.getElementById("Month").innerHTML= M;
	document.getElementById("Day").innerHTML= D;
	document.getElementById("Year").innerHTML= F;
	
	if (H > 12) 
	{
    H = H - 12;
    ap = "pm";

        if (H > 12) 
        {
        	H = H - 12;
            ap = "am";
    	}

        if (H == 0) 
        {
            H = 12;
        }

        if (Min < 10) 
        {
            Min = "0" + Min;
        }
    }   
    document.getElementById("Hours").innerHTML= + H + ":" + Min + " " + ap;	   
}

var newdate 

function DateDiff()
{
	newdate = (M + "/" + D + "/" + F);
	var diffDays = parseInt((someday - newdate) / (1000 * 60 * 60 * 24)); 
	alert(diffDays);

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

基本上,您需要将日期转换为以毫秒标记的int。然后找出差异。将它除以一天中的毫秒数。并将数字四舍五入到最近的int。

你可以这样做:

&#13;
&#13;
public void rethrowException(String exceptionName)
throws FirstException, SecondException {
  try {
    // ...
  }
  catch (Exception e) {
    throw e;
  }
}
&#13;
&#13;
&#13;