我有以下小提琴
https://jsfiddle.net/fNPvf/31895/
当我的日期如下var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,12);
时,我收到的警告为0
我希望显示为1
,以便有人帮助我
答案 0 :(得分:0)
我猜你想表明某事长达1天(日期相同),如果日期就像
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,13);
你想表明它是2天。
那么为什么不在diff中添加1呢? (如果我低估了你的目标)
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))) +1;
答案 1 :(得分:0)
function change(){
var oneDay = 24*60*60*1000;
var firstDate = new Date(2008,1,12);
var secondDate = new Date(2008,1,12);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
//to avoid 0 & less and make it 1 at least
//===========================================
diffDays = diffDays <= 0 ? 1 : diffDays;
//===========================================
alert(diffDays)
}
<input type="submit" id="byBtn" value="Change" onclick="change()"/>
答案 2 :(得分:-1)
如果您想要相同日期的差异,它将始终为零。为什么不应该呢? 无论如何,您可以检查结果是否为 0 ,然后将其设置为1.
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
diffDays=diffDays?diffDays:1;
alert(diffDays);