2014-01-20 29 views
2

您好目前我有一個JavaScript,顯示當前時間。是一個來自互聯網的例子。我該如何去做,以便當前顯示的時間比實際時間晚5分鐘。真的不知道時間是如何運作的。試圖改變一些數字,但無濟於事。當前時間(Javascript)在HTML

目前這是代碼。

$(document).ready(function() 
{ 
    var MONTHS = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ], 
     DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 

    var date = new Date(new Date().getTime() - (5 * 60 * 1000)); 

    $('#date').text(DAYS[date.getDay()] + " " + date.getDate() + ' ' + MONTHS[date.getMonth()] + ' ' + date.getFullYear()); 

    function zeroPad(val) { 
     return ((val < 10) ? "0" : "") + val; 
    } 

    setInterval(function() { 
     var fiveMinutesAgo = new Date(new Date().getTime() - (5 * 60 * 1000)); 
     $("#hours").text(zeroPad(fiveMinutesAgo.getHours())); 
     $("#min").text(zeroPad(fiveMinutesAgo.getMinutes())); 
     $("#sec").text(zeroPad(fiveMinutesAgo.getSeconds())); 
    }, 1000); 
}); 
+0

new Date()。getMinutes() - 5? – Dineshkumar

回答

1

儘量減不想要分鐘......

setInterval(function() { 
// Create a newDate() object and extract the minutes of the current time on the visitor's 
var minutes = new Date().getMinutes(); 
minutes = minutes - 5; // here you can minus or add minutes as per your requirements 
// Add a leading zero to the minutes value 
$("#min").html((minutes < 10 ? "0" : "") + minutes); 
},1000); 

修訂

var currDateTime = new Date(), 
    hr = currDateTime.getHours(), 
    min = currDateTime.getMinutes(), 
    dd = currDateTime.getDate(), 
    mm = currDateTime.getMonth() + 1, 
    yyyy = currDateTime.getFullYear(); 

//adding pad while getting singles in date or month between 0-9 
if(dd < 10){ dd = '0' + dd}  if(mm < 10){ mm = '0' + mm} 

//current date and time as per UTC format 
currDateTime = yyyy +'/'+ mm +'/'+ dd +" "+ hr + ':' + min + ":00"; 
$("#currDateTime").text(new Date(currDateTime).toUTCString()); 

//minus -5 minutes to actual date and time 
currDateTime = yyyy +'/'+ mm +'/'+ dd + " "+ hr + ':' + (min - 5) + ":00"; 
$("#minusDateTime").text(new Date(currDateTime).toUTCString()); 

//we are now going to add +5 min to currDateTime 
currDateTime = yyyy +'/'+ mm +'/'+ dd + " "+ hr + ':' + (min + 5)+ ":00"; 
$("#addDateTime").text(new Date(currDateTime).toUTCString()); 

這樣就可以增加或減去無的小時,分​​鍾,日,月,按要求每年對特定對象..

讓我知道進一步的澄清?

希望它會有所幫助! 感謝

Here is a JSFiddle demo

+0

如果我將分鐘數設置爲5,則會出現問題 - 5.因爲我爲每小時分鐘和秒設置了3個函數。如果分鐘少於5分鐘(例如12:03:01),它會顯示我(例如12:03-5:01),我該如何解決這個問題? – user3128861

+1

@ user3128861 - 這就是爲什麼在調用'getMinutes()'之前,應該從'Date'對象中減去五分鐘。您始終可以保留原始的Date對象(保存當前時間),並且還有一個保留時間減去五分鐘的Date對象。 –

+0

是啊!做到了。有用。謝謝! – user3128861

1

可以將日期轉換爲時間以毫秒爲單位,並且減去值得毫秒(1分鐘= 60K毫秒= 5分鐘/ 300K毫秒)的5分鐘。

var x = new Date; 
x = x.valueOf(); 
x -= 300000; 

然後轉換,但是你希望。

+0

我想顯示一個5分鐘的時間,但仍然保持當前時間 – user3128861

+0

因此設置2個變量? –

2

嘗試:

var nowMinusFiveMinutes = new Date(Date.now() - (5 * 60 * 1000)); 

注:

  1. 調用Date.now()得到,因爲 的毫秒爲單位的(1970年1月1日)的數量。
  2. (5 * 60 * 1000)是5分鐘內的毫秒數。
  3. 您可以使用new Date(numberOfMillisecondsSinceTheEpoch)構建日期。

UPDATE:

我看不出有三個獨立的setInterval()通話的需求。難道你不能在一次電話中更新三個元素,like this

+0

哦,我明白了。非常感謝!! – user3128861

+0

這是做到這一點的正確方法! – 2014-01-20 06:47:01

+0

@ user3128861 - 我很高興這有幫助,但是你不應該改變你的問題到答案中給出的問題。那個問題不再有意義了。 –

1

的代碼設定分鐘就行了

var minutes = new Date().getMinutes(); 

你可以做

var minutes = new Date().getMinutes() - 5; 

所以顯示的時間是實際時間晚5分鐘。我建議您閱讀this以瞭解有關Javascript的更多信息Date

+0

謝謝!有用! – user3128861

+0

我該如何去做,這樣它就不會得到負數。 :X – user3128861

+0

請參閱John的答案,這是正確的方式來做你想做的事情 – 2014-01-20 06:52:17