Angular Get Difference Between Two Dates in Days
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 12 CRUD Application Tutorial Example
- Angular 12 Httpclient Service Request Example
- How to Create Service in Angular 12?
- How to use Bootstrap Datepicker in Angular 12?
- Angular Material Mat-table Sticky Header Row Example
- Angular Material Mat Table Vertical Scroll Example
- Angular Material Carousel Slider Example
- How to Print a Page in Angular?
- Angular Doughnut Chart Example Tutorial
- Angular Pipe for Boolean Value with Yes No Text
- Angular Create Custom Pipe for Replace Null Values Example