Angular Get Difference Between Two Dates in Days

By Hardik Savani October 20, 2023 Category : Angular

This article is focused on angular get difference between two dates in days. I’m going to show you about angular get difference between two dates. This post will give you simple example of angular find difference between two dates. We will look at example of how to get difference between two dates in angular.

Here, i will give you simple two example how to get difference between two dates in days with angular. you can easily use this example with angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 version. let's see both example.

Example 1:

app.component.ts

import { Component, VERSION, OnInit } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

name = 'Angular ' + VERSION.major;

ngOnInit() {

console.log(this.getDiffDays('07/01/2021', '07/10/2021'));

}

getDiffDays(sDate, eDate) {

var startDate = new Date(sDate);

var endDate = new Date(eDate);

var Time = endDate.getTime() - startDate.getTime();

return Time / (1000 * 3600 * 24);

}

}

Output:

9

Example 2:

app.component.ts

import { Component, VERSION, OnInit } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']

})

export class AppComponent implements OnInit {

name = 'Angular ' + VERSION.major;

ngOnInit() {

console.log(this.getDiffDays(new Date('07/01/2021'), new Date('07/10/2021')));

}

getDiffDays(startDate, endDate) {

return Math.ceil(Math.abs(startDate - endDate) / (1000 * 60 * 60 * 24));

}

}

Output:

9

i hope it can help you...

Tags :
Shares