您好,目前我有一个显示当前时间的JavaScript。是一个来自互联网的例子。我该如何做,以使当前显示的时间比实际时间晚5分钟。真的不知道时间如何运作。试图更改一些数字,但无济于事。
目前这是代码。
$(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);
});
尝试减去您想要的分钟数...
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());
这样,您可以根据要求向特定对象添加或减去小时,分钟,天,月,年中的任意一个。
让我知道进一步的澄清吗?
希望这会有所帮助!谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句