2016-09-06 39 views
0

試圖找出2個日期摸出年:月:日離開

我想我靠近之間剩下的時間,但我得到了我的天,年故障的結果,我也檢查過其他解決方案,但它們是不同於我嘗試這樣做的方式......我可以根據自己的方式做我需要的嗎?

var seconds= Math.floor((timeLeft/1000)%60); 
var minutes= Math.floor((timeLeft/(1000*60))%60); 
var hours= Math.floor((timeLeft/(1000*60*60))%24); 
var dayz= Math.floor((timeLeft/(1000*60*60*24))%31); 
var years= Math.floor((timeLeft/(1000*60*60*24*31))%12); 

$(document).ready(function(){ 
 

 
var currentDate = new Date(2017, 08, 07 , 14 , 33, 20); //set fixed dates to test with 
 
var futureDate = new Date(2018, 08, 07 , 14 , 33, 20); //set fixed dates to test with 
 

 
var countDown = setInterval(function() { 
 
    
 
    
 
\t \t \t var currentD = new Date(2017, 08, 07 , 14 , 33, 20); //set fixed dates to test with 
 
\t \t \t var timeLeft = new Date(); 
 
\t \t \t timeLeft = futureDate - currentD; 
 
    
 
      var seconds= Math.floor((timeLeft/1000)%60); 
 
\t \t \t var minutes= Math.floor((timeLeft/(1000*60))%60); 
 
\t \t \t var hours= Math.floor((timeLeft/(1000*60*60))%24); 
 
\t \t \t var dayz= Math.floor((timeLeft/(1000*60*60*24))%31); 
 
      var years= Math.floor((timeLeft/(1000*60*60*24*31))%12); 
 
    
 
    
 
     $('#timeLeft').text(years + " " + dayz + " " + hours + " " + minutes + " " + seconds); 
 

 
    
 
    
 
}, 1000); 
 

 

 
    $('#currentDate').text(currentDate); 
 
    $('#futureDate').text(futureDate); 
 
    
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
Current Date: <span id="currentDate"></span><br/> 
 
Future Date: <span id="futureDate"></span><br/> 
 
Time till: <span id="timeLeft"></span><br/>

+0

我想這個問題是,你假設平均每月天都31時,而不是在30.41。 – zangarmarsh

回答

0

我建議使用momentjs代替。這將使事情很簡單......下面會給你在天的差異,但你可以很容易地爲您的區別每個可能的格式:

var currentDate = moment([2017, 8, 7 , 14 , 33, 20]); //set fixed dates to test with 
 
var futureDate = moment([2018, 8, 7 , 14 , 33, 20]); 
 
var format = "YYYY-MM-DD HH:mm:ss"; 
 
document.getElementById("currentDate").appendChild(document.createTextNode(currentDate.format(format))); 
 
document.getElementById("futureDate").appendChild(document.createTextNode(futureDate.format(format))); 
 

 
var difference = futureDate.diff(currentDate, 'days', true); 
 
var durationReadable = moment.duration(futureDate.diff(currentDate)).humanize(true); 
 

 
document.getElementById("timeLeft").appendChild(document.createTextNode(difference)); 
 
document.getElementById("duration").appendChild(document.createTextNode(durationReadable));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.min.js"></script> 
 

 
Current Date: <span id="currentDate"></span><br/> 
 
Future Date: <span id="futureDate"></span><br/> 
 
Time till: <span id="timeLeft"></span><br/> 
 
Time till humanized: <span id="duration"></span>

+0

非常好的腳本,但我正在學習,所以我需要學習艱難的方法,並弄清楚如何自己構建它;) –

0

我得到了它, 這裏爲任何像我這樣的新手提供解決方案。

$(document).ready(function(){ 
 
var showOne = new Date(); 
 
var showTwo = new Date(2016, 08, 14); 
 
$('#showOne').text(showOne); 
 
$('#showTwo').text(showTwo); 
 

 

 
setInterval(function(){ 
 
var currentDateSec = $.now()/1000 ; 
 
var laterDate = showTwo; 
 
var laterDateSec = laterDate/1000; 
 
var timeLeft = laterDateSec - currentDateSec; 
 

 
var sLeft = Math.floor(timeLeft) %60; 
 
var mLeft = Math.floor(timeLeft/(60)) %60; 
 
var hLeft = Math.floor(timeLeft/(60 * 60)) %24; 
 
var dLeft = Math.floor(timeLeft/(60 * 60 * 24)) %7; 
 
var wLeft = Math.floor(timeLeft/(60 * 60 * 24 * 7)); 
 

 

 
    $('#currentDate').text(currentDateSec); 
 
    $('#futureDate').text(laterDateSec); 
 
    $('#timeLeft').text(timeLeft); 
 
    
 
    $('#sLeft').text(sLeft); 
 
    $('#mLeft').text(mLeft); 
 
    $('#hLeft').text(hLeft); 
 
    $('#dLeft').text(dLeft); 
 
    $('#wLeft').text(wLeft); 
 
    
 
$('#humandate').text(wLeft + " Weeks - " + dLeft + " Days - " + hLeft + " Hours - " + mLeft + " Mins - " + sLeft + " Sec\(s\)"); 
 
    
 
    
 
}, 1000); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span id="showOne"></span><br/> 
 
<span id="showTwo"></span><br/> 
 
<br/><br/> 
 
Current Date: <span id="currentDate"></span><br/> 
 
Future Date: <span id="futureDate"></span><br/> 
 
Seconds till: <span id="sLeft"></span><br/> 
 
Mins till: <span id="mLeft"></span><br/> 
 
Hours till: <span id="hLeft"></span><br/> 
 
Days till: <span id="dLeft"></span><br/> 
 
Weeks till: <span id="wLeft"></span><br/> 
 
<br/><br/> 
 
<span id="humandate"></span><br/>