1

I'm new to angular, I would like to know if there is there a way to calculate the difference between a specific date and the current date, and then start counting the time from that difference?

Example: 29/01/2020 21:00:00 - 29/01/2020 21:30:00 gives a difference of 30 minutes ... the count should start from 30 minutes, that is 00:30:00

Demo

Code

startTime(){
  this.interval = setInterval(() => {
      if (this.time === 0) {
        this.time++;
      } else {
        this.time++;
      }
      this.display = this.time;

      return this.display;
    }, 1000);
}
Sherman Hui
  • 938
  • 3
  • 10
  • 22
Mat Sigh
  • 71
  • 1
  • 10
  • Does this answer your question? [Javascript simple count up with delay](https://stackoverflow.com/questions/27588551/javascript-simple-count-up-with-delay) – Tristan Jan 29 '20 at 21:54
  • @TristanWiley Thank you for your help. It doesn't solve my problem because I can start a count, I can't start that count through the result of two dates – Mat Sigh Jan 29 '20 at 21:59
  • So are you looking to determine the difference between two dates? Or something else – Tristan Jan 29 '20 at 22:01
  • @TristanWiley I want the difference between two dates to start the counter later, not at 0, but from the subtraction value – Mat Sigh Jan 29 '20 at 22:06

5 Answers5

1

You could compute a difference between the two dates in milliseconds using Date.getTime(). If you create a new Date object from this difference, it will contain a representation of this interval. So you only need to increment the seconds and display the formatted time:

// difference between two dates in milliseconds
const diff = new Date('May 1,2019 11:20:00').getTime() - new Date('May 1,2019 11:00:00').getTime();
// new date object created from this difference
const start = new Date(diff);
const el = document.getElementById('time');
setInterval(() => {
  // updating the time every second
  start.setSeconds(start.getSeconds() + 1);
  el.innerHTML = `${format(start.getUTCHours())}: ${format(start.getUTCMinutes())}: ${format(start.getUTCSeconds())}`;
}, 1000)

function format(n) {
  return n < 10 ? '0' + n : '' + n;
}
<div id=time></div>
Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
0

I would recommend you try moment (https://momentjs.com/docs/#/displaying/difference/).

With that you can easilly get the difference in milliseconds:

const currentTime = moment();
const someTime = moment([2010, 1, 14, 15, 25, 50, 125]) // [year, month, day, hour, minute, second, millisecond]

const millisecondDifference = currentTime.diff(someTime);

and then use that difference to set interval (or use moment/Date to transform it to something)

Kaca992
  • 2,211
  • 10
  • 14
0

You don't need to use a counter, and a counter will probably not give you the result you want as setInterval/setTimeout will not fire at exactly 1000ms.

You can subtract the start date from the current date every time setInterval calls your function. then format the result:

var start = new Date('2020-01-29 21:00:00');
startTime(){
  this.interval = setInterval(() => {
      this.display = new Date() - start;

      return this.display;
    }, 1000);
}
sney2002
  • 856
  • 3
  • 6
0

After subtracting the current date from the given date, in milliseconds, convert it to seconds, minutes , and hours and use setInterval to update the counter.

const date = Date.parse('30 Jan 2020 01:04:56 GMT+0300');  //your given date
const elem = document.getElementById('counter')

function count() {

  let countFrom = (Date.now() - date); //get the difference with the current date
  
  //convert to seconds, minutes and hours
  seconds = parseInt((countFrom/1000) % 60)
  minutes = parseInt((countFrom/(1000*60)) % 60)
  hours = parseInt((countFrom/(1000*60*60)) % 24);

  //append `0` infront if a single digit
  hours = (hours < 10) ? "0" + hours : hours;
  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;
 
  let time = `${hours}:${minutes}:${seconds}`;
  elem.textContent = time;
  
}

setInterval(count, 1000);
<div id='counter'>00:00:00</div>
Addis
  • 2,480
  • 2
  • 13
  • 21
0

I think this solves your problem

let now = Date.now();
let future = new Date('January 29, 2020 23:59:59').getTime();

let diffInSecond = Math.floor((future - now) / 1000);
var i = diffInSecond;
var interval = setInterval(function(){ 
    if (i == 0) clearInterval(interval);
    console.log(i);
    i--;
}, 
1000);

Everything is in second you can format the result to show something like 00:31:29

Wonkledge
  • 1,732
  • 7
  • 16