ItSolutionStuff.com

Angular Get Difference Between Two Dates in Days

By Hardik Savani • May 2, 2024
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: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Angular 12 CRUD Application Tutorial Example

Read Now →
ā˜…

Angular 12 Httpclient Service Request Example

Read Now →
ā˜…

How to Create Service in Angular 12?

Read Now →
ā˜…

How to use Bootstrap Datepicker in Angular 12?

Read Now →
ā˜…

Angular Material Mat-table Sticky Header Row Example

Read Now →
ā˜…

Angular Material Mat Table Vertical Scroll Example

Read Now →
ā˜…

Angular Material Carousel Slider Example

Read Now →
ā˜…

How to Print a Page in Angular?

Read Now →
ā˜…

Angular Doughnut Chart Example Tutorial

Read Now →
ā˜…

Angular Pipe for Boolean Value with Yes No Text

Read Now →
ā˜…

Angular Create Custom Pipe for Replace Null Values Example

Read Now →