2

I have a date in the following format: date: "2020-02-03T10: 00: 00";

Is there a way to calculate the difference between the current time - 2020-02-03T10: 00: 00?

Then I intend to start a timer that starts counting from the result. If the result is 30, I want the timer to start at 00:30:00, if it is 60, start 01:00:00.

Can someone help me?

BLITZ

code

data = [
    {
      date: "2020-02-03T10:00:00",
    },
  ];

  start(){
     this.interval = setInterval(() => {
     this.display = Date.now() - parseInt(this.data[0].date);
     console.log(this.display)

          }, 1000);
  }
Harry
  • 621
  • 3
  • 9
  • 21

4 Answers4

2

You need to convert your String date: "2020-02-03T10:00:00" to a Date Object in order to calculate the difference.

You can use new Date(dateString). An example could be:

data = [
{
  date: "2020-02-03T10:00:00",
},
];

start(){
 this.interval = setInterval(() => {
 var date2 = new Date(this.data[0].date)
 this.display = Date.now() - date2;
 console.log(this.display)

      }, 1000);
}

Mind that the result is the difference in milliseconds.

PaulS
  • 850
  • 3
  • 17
  • The time format is not displayed correctly, but thanks for trying to help me! – Harry Feb 03 '20 at 12:56
  • @Harry It IS displayed correctly. As I said, it's in milliseconds. For me it's 14:00 right now. The Date from data is 10:00. Trying the code, I get `14377876`, which is 240 minutes, which is 4 hours. – PaulS Feb 03 '20 at 13:01
  • I add a datepipe and perfect ! Thank you – Harry Feb 03 '20 at 13:06
1

I believe this is something you want to have in your function. Tested and working: :)

    let difference = (+Date.now()) - (+new Date(this.data[0].date)) / 1000;
    this.interval = setInterval(() => {
      difference--; // difference++ for increasing values if wanted to count in different way
      const s = Math.floor(difference / 1) % 60;
      const m = Math.floor(difference / 60) % 60;
      const h = Math.floor(difference / (60 * 60)) % 24;
      this.display = `${h}:${m}:${s}`;
    }, 1000);

first convert to timestamp both dates. Substract and get difference and divide it by 1000 to get seconds. On each inteval decrease seconds by 1. Afterwards, get hours (const h), minutes(const m) and seconds(const s) by using formula above. Print out in display(this.display) prop.

Nikola Stekovic
  • 635
  • 3
  • 13
  • The problem is that the count is regressing instead of increasing and the time difference is not correct, but it was very close to what I intend to achieve! Thank you so much for trying to help me! – Harry Feb 03 '20 at 12:58
  • If you want to increase time just change to `difference++;` and also you should work with UTC dates in order to have it correct. Cheers – Nikola Stekovic Feb 03 '20 at 13:07
0

Moment.js is a good library for this.

Here is an example for your purpose:

//  Get date from string

var date = moment("2020-02-03T10:00:00");
var now = moment();

//  Get diference in milliseconds --> https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/07-difference/

var diference = now.diff(date, 'miliseconds');

//  Timeout in milliseconds --> https://www.w3schools.com/jsref/met_win_settimeout.asp

setTimeout(function(){ alert("Hello"); }, diference);
Dani
  • 1,825
  • 2
  • 15
  • 29
0

You can easily manipulate dates using moment library, like :

const data = [{
  date: '2020-02-03T14:00:00',
}];

this.interval = setInterval(() => {
  const [d] = data;

  this.display = moment() - moment(d.date);

  console.log(this.display);
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

The displayed result is the difference in milliseconds. So if you want seconds, divide the result by 1000. Negative number will say the date is in the future, positive that it's in the past.

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69