Date difference in hours - Angular 2

后端 未结 3 805
不知归路
不知归路 2020-12-11 05:39

How can I get difference in hours between two dates in angular2? I don\'t want to use external libs like moment.js.

Having, for example: incidentTi

3条回答
  •  攒了一身酷
    2020-12-11 06:22

    This should do the job:

    const date1 = params.data.incidentTime;
    const date2 = params.data.creationTime;
    
    const diffInMs = Date.parse(date2) - Date.parse(date1);
    const diffInHours = diffInMs / 1000 / 60 / 60;
    
    console.log(diffInHours);
    

    Use Math.floor to round the result down, or Math.ceil to round it up

提交回复
热议问题