当前时间(Javascript)在HTML中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当前时间(Javascript)在HTML中相关的知识,希望对你有一定的参考价值。

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

这样,您可以根据要求向特定对象添加或减去小时,分钟,日,月,年。

让我知道进一步澄清?

希望它会有所帮助!谢谢

Here is a JSFiddle demo

另一答案

尝试:

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

另一答案

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

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

然后根据你的意愿转换它。

另一答案

代码正在设置分钟

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

你可以做

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

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

以上是关于当前时间(Javascript)在HTML中的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript中代码注释的正确方式是啥

调整 JavaScript 中代码排序的输入

Javascript中代码的执行顺序是啥?

码片速率的含义

码分复用

转载 WCDMA中码片速率符号速率bit速率 WCDMA常用概念