[英]Calculating between dates
到目前為止,這是我使用jquery所擁有的。 我不知道為什么我的checkRecord按鈕不會顯示兩個日期之間的天數。 我顯然缺少了一些東西。
$(document).ready(function () {
'use strict';
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());
$('#safetyRecord').hide();
$('#today').html(dayNames[newDate.getDay()] + "," +' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getDate() + ","+ ' ' + newDate.getFullYear());
$('#checkRecord').click(function(){
var $daysSinceLastAccident = $('#daysSinceLastAccident');
var dateOfLastAccident = new Date($('#dateOfLastAccident').val());
var today = new Date();
$daysSinceLastAccident = Math.floor((today.getTime() - dateOfLastAccident.getTime()) / (24 * 60 * 60 * 1000));
$daysSinceLastAccident.text(daysSinceLastAccident);
$('#safetyRecord').show();
});
});
嘗試更改:
var today = new date;
和$('#safetyRecord').show;
至
var today = new Date();
和$('#safetyRecord').show();
在您的代碼中:
> newDate.setDate(newDate.getDate());
什么也不做,只是將newDate的日期設置為其當前值。
> var $daysSinceLastAccident = $('#daysSinceLastAccident');
> [...]
> $daysSinceLastAccident = Math.floor((today.getTime() - ...
最初為$ daysSinceLastAccident分配了對jQuery對象的引用,然后是一個數字。 初次分配的目的是什么?
> $daysSinceLastAccident.text(daysSinceLastAccident);
但是您似乎希望它仍然是jQuery對象。
也:
> var today = new Date();
已經有一個newDate變量,它可能具有完全相同的值,可能早於1ms,該變量已用於顯示當前日期。 似乎沒有任何理由不再次使用它。
請注意,如果希望兩個日期對象之間的天數差,可以將時間設置為相關日期的正午,然后將其減去並除以ms /天,然后四舍五入。
例如:
var newDate = new Date();
newDate.setHours(12,0,0,0); // 12:00:00.000
// You should never leave parsing of date strings to the Date object
// I'll assume here that it "works" in the limited cases you've tested
var dateOfLastAccident = new Date($('#dateOfLastAccident').val());
dateOfLastAccident.setHours(12,0,0,0);
var daysSinceLastAccident = Math.round((newDate - dateOfLastAccident) / 8.64e7);
您應該手動解析日期字符串,這並不難。 例如,像2013-10-09這樣的輸入,您可以執行以下操作:
function parseISODateString(s) {
s = s.split(/\D/g);
var d = new Date(s[0], --s[1], s[2]);
// Return d if s was a valid date string, NaN otherwise.
return (d && d.getFullYear() == s[0] && d.getDate() == s[2])? d : NaN;
}
您應該測試輸入字符串和返回值以使其健壯。
這是獲取最差日期持續時間(以年,月和日為單位)的最簡單函數...檢查此代碼
function getDatesDiff(from,to){
from = new Date(from);
to = new Date(to);
if (isNaN(to - from)) return "";
from.setDate(from.getDate()-1);
var fullMonths=0;
var days=0;
var fromDate=new Date(from);
var finalDate=new Date(fromDate);
var nextMonth=new Date(new Date(fromDate).setMonth(new Date(fromDate).getMonth() + 1));
var result='';
while(from<=to){
if(+from == +nextMonth){
fullMonths++;
finalDate=new Date(nextMonth);
nextMonth.setMonth(nextMonth.getMonth() + 1);
}
from.setDate(from.getDate() + 1);
}
days=Math.round((to-finalDate)/(1000*60*60*24));
if(Math.round(fullMonths/12)>0)
result=Math.round(fullMonths/12) + "year"+(Math.round(fullMonths/12)>1?'s':'');
if(parseInt(fullMonths%12)>0)
result+=" "+fullMonths%12 + "month"+(parseInt(fullMonths%12)>1?'s':'') ;
if(days>0)
result+=" "+ days + "day"+(days>1?'s':'');
return result;
}
這是jsfiddle .......
https://jsfiddle.net/1rxnmvnd/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.