JavaScript代码运行时钟(日期和时间)快四倍 [英] JavaScript code to run the Clock (date and time) four times faster

查看:79
本文介绍了JavaScript代码运行时钟(日期和时间)快四倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要虚拟时间(4 x当前日期和时间)。我已设法显示当前日期和时间的运行时钟,但我无法将时间比当前时间快四倍。


例如,如果当前日期是01-01-2012 00:00:00,虚拟时间应为
01-01-2012 00:00:04




不仅单独的秒数应该相乘;当秒数超过59个虚拟秒时,相应的分钟数,小时数,日期,月份和年份也应该相乘。也就是说,时钟应该以我的日期格式以每秒4秒的增量运行。



请参阅我的页面: http://www.chemfluence.org.in/sample.html



<现在它正在运行当前时间。

请参阅下面的代码。

 <!DOCTYPE html> 
< html>
< head>
< script>
函数startTime()
{
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//在数字前面加零'< 10
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt')。innerHTML =
today.getDate()+
- +
(today.getMonth()+ 1)+ - +
today.getFullYear()+
+ h +:+ m +:+ s;
t = setTimeout(function(){startTime()},500);


函数checkTime(i)
{
if(i <10)
{
i =0+ i;
}
返回i;
}
< / script>
< / head>

< body onload =startTime()>
< div id =txt>< / div>
< / body>
< / html>


解决方案

有一个简单的公式来确定虚拟时间每一个给定的时间,知道两个时间戳和因素:

  var virtualOrigin = Date.parse(2012-01-01T00: 00:04),
realOrigin = Date.parse(2012-01-01T00:00:00),
factor = 4;

函数getVirtual(time){
return new Date(virtualOrigin +(time - realOrigin)* factor);
}

//用法:
var now = new Date(),
toDisplay = getVirtual(now);

在jsfiddle.net的演示


I am in need of virtual time (4 x current date and time). I have managed to display the running clock with the current date and time, but I am unable to the time four times faster than current time.

For example, if the current date is 01-01-2012 00:00:00, the virtual time should be 01-01-2012 00:00:04

Not only the seconds alone should get multiplied; the corresponding minutes, hours, date, month and year also should get multiplied when seconds crosses 59 virtual seconds. That is, the clock should run live with incremental of four seconds for every second with my date format.

Please see my page: http://www.chemfluence.org.in/sample.html

It's now running with the current time. I want to run this four times faster.

Please see my code below.

<!DOCTYPE html>
<html>
    <head>
        <script>
            function startTime()
            {
                var today = new Date();
                var h = today.getHours();
                var m = today.getMinutes();
                var s = today.getSeconds();
                // Add a zero in front of numbers<10
                m = checkTime(m);
                s = checkTime(s);
                document.getElementById('txt').innerHTML =
                    today.getDate() +
                    "-" +
                    (today.getMonth()+1)+"-" +
                    today.getFullYear() +
                    " "+h+":"+m+":"+s;
                t = setTimeout(function(){startTime()},500);
            }

            function checkTime(i)
            {
                if (i<10)
                {
                    i = "0" + i;
                }
                return i;
            }
        </script>
    </head>

    <body onload="startTime()">
        <div id="txt"></div>
    </body>
</html>

解决方案

There is a simple formula to determine the virtual time for every given time, knowing the two timestamps and the factor:

var virtualOrigin = Date.parse("2012-01-01T00:00:04"),
    realOrigin = Date.parse("2012-01-01T00:00:00"),
    factor = 4;

function getVirtual(time) {
    return new Date( virtualOrigin + (time - realOrigin) * factor );
}

// usage:
var now = new Date(),
    toDisplay = getVirtual(now);

Demo at jsfiddle.net

这篇关于JavaScript代码运行时钟(日期和时间)快四倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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