The left -hand and right hand side of an arithmeti

2020-05-30 03:21发布

I am getting the following error. I am not able to find out where exactly i went wrong.Can someone help me out with the solution

enter image description here

The code

 function() {
    this.devices.forEach(device => {
      let lastConnect = device.lastConnection.split('+');
      lastConnect = lastConnect[0] + 'Z';
      let diff = Math.abs(new Date() - new Date(lastConnect));//getting error here
}

4条回答
放我归山
2楼-- · 2020-05-30 03:38

I have found out the issue.

This code work only in Javascript

Math.abs(new Date() - new Date(lastConnect)) .

Inorder to make it work in Typescript. Update the code as shown below

Math.abs(Date().getTime() - new Date(lastConnect).getTime());
查看更多
狗以群分
3楼-- · 2020-05-30 03:39

No need for Math.abs() to answer this question...

Just using getTime() method converts a Date into a number (Date.prototype.getTime()) so you can make the operation without that error

Check on this example

查看更多
Rolldiameter
4楼-- · 2020-05-30 03:51

The simplest answer would be

Math.abs(<any>new Date() - <any>new Date(lastConnect));
查看更多
SAY GOODBYE
5楼-- · 2020-05-30 03:54

Another great way:

Math.abs((new Date() as any) - (new Date(lastConnect) as any));

查看更多
登录 后发表回答