根据用户输入的 javascript 计算运行时间



基本上,我试图让用户以 mm/dd/yyyy 的形式输入日期。它可以是任何日期,但他们必须选择它。我想做的是计算自他们输入的日期以来经过的年、月和天数。我已经到了他们可以选择一个日期的地步,但除此之外,我被难住了。这是我得到的:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">
<Title>Elapsed Time Calculator</Title>
<script src="modernizr.custom.05819.js"></script><!--Links to file containing modernizer library-->
<body>
<!-- Navigation -->
    <nav>
      <ul class="w3-navbar w3-black">
        <li><a href="file:///C:/Users/Kyle/Desktop/Document1.html">Home</a></li> <!--Link to Home Page-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document2.html">NHL Teams</a></li><!--Link to Page of NHL Teams-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document3.html">AHL Teams</a></li><!--Link to Page of AHL Teams-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document4.html">WHL Teams</a></li><!--Link to Page of WHL Teams-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document5.html">G.A.A. Calculator</a></li><!--Link to Page of WHL Teams-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document6.html">Fan Survey</a></li><!--Link to Fan Survey Page-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document7.html">Web Safety</a></li><!--Link to Page about Web Safety-->
        <li><a href="file:///C:/Users/Kyle/Desktop/Document8.html">Elapsed Time</a></li><!--Link to Page That Calculates Elapsed Time Between Two Dates-->
      </ul>
    </nav>
    <header>
             <h1 style="text-align:center;">Elapsed Time</h1>
    </header>
    <article>
        <form>
          <fieldset>
            <label for="dateSelected">
              Select a date
            </label>
            <input type="date" id="dateSelected" />
          </fieldset>
          <label for "timeElapsed"> Time Elapsed: </label>
          </form>
</article>
</body>
</html>

如果我没记错的话,你这样做的方法是将日期转换为毫秒,然后从该数字转换,但是当涉及到用户输入的数据时,我不太确定如何做到这一点。有人可以帮忙吗?

使用此处描述的方法之一创建一个新日期 http://www.w3schools.com/js/js_dates.asp 然后使用此问题中的代码 计算两次之间的时差 JavaScript

最新更新