【问题标题】:Current Time (Javascript) In HTMLHTML 中的当前时间 (Javascript)
【发布时间】:2014-01-20 05:45:25
【问题描述】:

您好,目前我有一个显示当前时间的 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);
});

【问题讨论】:

  • new Date().getMinutes()-5 ?

标签: javascript jquery html time


【解决方案1】:

试试:

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

注意:

  1. 调用Date.now() 得到毫秒数 时代(1970 年 1 月 1 日)。
  2. (5 * 60 * 1000) 是 5 分钟内的毫秒数。
  3. 您可以使用new Date(numberOfMillisecondsSinceTheEpoch) 构造日期。

更新:

我认为不需要三个单独的setInterval() 调用。您不能一次调用更新三个元素吗,like this

【讨论】:

  • 哦,我明白了。非常感谢!
  • @user3128861 - 我很高兴这对您有所帮助,但您不应该将您的问题更改为答案中给出的内容。那么这个问题就没有意义了。
  • 对不起我的错。最后一个问题。我如何冻结时间?我试过 clearInterval() 但它似乎没有工作
  • @user3128861:要清除间隔,您需要设置 id 并清除该间隔。因此,请尝试将整个代码放在一个单独的函数中,并在您想使用的任何地方调用它。假设您将函数设置为 function testInterval() { your date time code here...} 然后在它旁边使用 var id = testInterval(); window.clearInterval(id);.它应该可以解决您的问题。
  • @user3128861 - 我更新了fiddle 以显示如何清除间隔。
【解决方案2】:

尝试减去你想要的分钟数......

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

【讨论】:

  • 如果我输入分钟 - 5 会有问题,因为我为每小时分钟和秒设置了 3 个函数。如果分钟少于 5 分钟(例如 12:03:01),它会告诉我(例如 12:03-5:01)我该如何解决这个问题??
  • @user3128861 - 这就是为什么您应该在调用getMinutes() 之前从Date 对象中减去五分钟。您可以始终保留原始的 Date 对象(保存当前时间),也可以拥有一个保存时间减去五分钟的 Date 对象。
  • @user3128861 - John S 是对的!以简单的方式查看我的更新答案。
  • @John S - 感谢他的澄清。
【解决方案3】:

您可以将日期转换为以毫秒为单位的时间,然后减去 5 分钟的毫秒数(1 分钟 = 60k 毫秒 = 5 分钟/300k 毫秒)。

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

然后随心所欲地转换它。

【讨论】:

  • 我想显示一个落后 5 分钟的时间,但仍然保留当前时间
【解决方案4】:

代码是设置分钟就行了

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

你可以的

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

所以显示的时间比实际时间晚 5 分钟。我建议您阅读 this 以了解有关 Javascript 的更多信息 Date

【讨论】:

  • 我该怎么做才不会得到负数。 :X
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2016-08-13
  • 2015-06-18
  • 2014-11-04
  • 1970-01-01
相关资源
最近更新 更多