1

This is the sample code that i am using which was again came as an answer in the stack, but however, it multiplies the date entirely though the time works faster at the given factor 5.

How to start the time from the current system time and run it faster at 5 times speed simultaniously increasing the date and the time.

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

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

    function pad2(num) {
        return ("0"+num).substr(-2);
    }
    function format(time) {
         return time.getDate()
              + ":" + pad2(time.getMonth()+1)
         + ":" +  pad2(time.getYear())

          + "     " + pad2(time.getHours())
            + ":" + pad2(time.getMinutes())
            + ":" + pad2(time.getSeconds());
    }

    function startTime() {
        var now = new Date();
        var display = getVirtual(now);
        output.innerText = format(display);
        setTimeout(startTime, 1000/factor - (now.getMilliseconds() % (1000/factor)));
    }

    var output = document.getElementById("txt");
    startTime();
  • 4
    Possible duplicate of [JavaScript code to run the Clock (date and time) four times faster](https://stackoverflow.com/questions/14272133/javascript-code-to-run-the-clock-date-and-time-four-times-faster) – Nope May 31 '18 at 11:55
  • @Nope this as it states on the question it self is used by the link you have mentioned. however, it has an issue that i would like your help if you know or someone who is knowledgable that can answer – Avishka Kodippilli May 31 '18 at 11:59
  • What is the problem you are facing? – AvcS May 31 '18 at 12:04
  • If you want to increase the date as well, I assume `now = now.setDate(now.getDate() + factor);` inside your `start_time`? Example seen here: [https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript](https://stackoverflow.com/questions/3674539/incrementing-a-date-in-javascript) – Nope May 31 '18 at 12:05
  • **26:01:44 16:29:16** this is the time and date it shows when i run the code. – Avishka Kodippilli May 31 '18 at 12:15

1 Answers1

0

Is this what you are looking for?

function pad2(num) {
    return ('0' + num).substr(-2);
}

function format(time) {
    return (
        time.getDate()
            + ':' + pad2(time.getMonth() + 1)
            + ':' + pad2(time.getYear())
            + '     ' + pad2(time.getHours())
            + ':' + pad2(time.getMinutes())
            + ':' + pad2(time.getSeconds())
    );
}

var originalTime = Date.now();
var original = document.getElementById("original");
setInterval(function () {
    originalTime += 1000;
    original.innerText = format(new Date(originalTime));
}, 1000);

var factor = 5;
var fastTime = Date.now();
var fast = document.getElementById("fast");
setInterval(function () {
    fastTime += 1000;
    fast.innerText = format(new Date(fastTime));
}, 1000 / factor);
<p> Original Timer </p>
<pre id="original"></pre>
<br />
<p> Fast Timer </p>
<pre id="fast"></pre>
AvcS
  • 2,263
  • 11
  • 18