In this post, we find a difference between two dates in days

TypeScript File

var timeStart= new Date(this.startDate).getTime();
var timeEnd = new Date(this.endDate).getTime();
var diff = timeEnd - timeStart; //in ms
var milliseconds = timeStart > timeEnd ? timeStart % timeEnd : timeEnd % timeStart;
var seconds = milliseconds / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var weeks = days / 7;
var totalDays = Math.ceil(days);
this.result = totalDays + ' Days';
// console.log('Days',days.toFixed(2));
console.log('Total Days',totalDays);

HTML File

<h1>Difference b/w two dates in days</h1>

<p>Start Date - <b>{{startDate}}</b></p>
<p>End Date - <b>{{endDate}}</b></p>
<p>Result - <b>{{result}}</b></p>

Result

StackBiltz working example here Open StackBiltz

Post your comment

Your email address will not be published.