HTML中的当前时间(JavaScript) [英] Current Time (Javascript) In HTML

查看:49
本文介绍了HTML中的当前时间(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,目前我有一个显示当前时间的JavaScript.是一个来自互联网的例子.我该如何做,以使当前显示的时间比实际时间晚5分钟.真的不知道时间如何运作.试图更改一些数字但无济于事.

Hi currently I had a javascript that display current time. Is a example taken from the internet. How do I go about doing it such that the current time displayed is 5mins behind the actual time. Don't really know how the time works. Tried to change some numbers but to no avail.

当前这是代码.

 $(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);
});

推荐答案

尝试减少您想要的分钟数...

try to minus no of minutes that you want to...

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());

通过这种方式,您可以根据需要向特定对象添加或减去小时,分钟,天,月,年中的任意一个.

This way you can add or minus no of hours, minutes, days, months, years to specific objects as per requirements..

让我知道进一步的澄清吗?

Let me know for further clarifications?

希望这会有所帮助!谢谢

Hope it would helps! Thanks

这是一个JSFiddle演示

这篇关于HTML中的当前时间(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆