我真的需要幫助來計算兩個日期之間的差異,它必須是整數中的天數。這裏是我的HTML和jQuery ..使用查詢計算日期之間的差異
這是不工作的...該網站應該說今天的日期。然後用戶會選擇日期並點擊「檢查我們的安全記錄」按鈕,並在頁面底部顯示它已經有多少天。 jQuery的:
$(document).ready(function() {
$('#safetyRecord').hide();
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var newDate = new Date();
newDate.setDate(newDate.getDate());
$('#today').html(dayNames[newDate.getDay()] + "," +' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + ","+ ' ' + newDate.getFullYear());
/*
$(':button').click(function(){
var start = $('#dateOfLastAccident').val();
var end = $('#today');
var startDate = new Date(start);
var endDate = new Date(end);
var diff = endDate - StartDate;
var numDays = Math.floor(diff/1000 /60 /60 /24);
$('#daysSinceLastAccident'+ numDays)();
*/
});
的HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>generic safety</title>
<link rel="stylesheet" href="L3hw.css">
<!-- jQuery library hosted by Google Content Distribution Network -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="L3hw.js"></script>
</head>
<body>
<header>
<img src="images/genericBanner.png" alt="Generic, Inc.">
<p>
The New Ventures Mission is to scout profitable growth opportunities in
relationships, both internally and externally, in emerging, mission-inclusive
markets, and explore new paradigms and then filter and communicate and
evangelize the findings.
</p>
</header>
<section id="main">
<h1>Safety at generic company</h1>
<h2>Today is <span id="today">TBD</span>.<br>
Our last lost-work accident was on
<input type="date" id="dateOfLastAccident">
<button type="button" id="checkRecord">Check our safety record</button>
</h2>
<h2 id="safetyRecord">
It has been <span id="daysSinceLastAccident">TBD</span> days since our last lost-work accident.
</h2>
</section> <!-- end main -->
<footer>
<p id="message">
<!-- (an appropriate safety message will appear here) -->
</p>
<p><img src="images/genericBullet.png" alt="*"> Generic Company - we do stuff</p>
</footer>
</body>
</html>
所有帽子都像是SHOUTING ...請不要放棄! – tjons
這可能會給你一些見解':)''http://stackoverflow.com/questions/10931288/how-to-add-subtract-dates-with-javascript/10931362#10931362 –
對不起,我從來沒有使用過這個網站,我試圖將它從代碼中分離出來。我不使用日期選擇器我只是在HTML中使用輸入類型=日期 – user2856563