0

I have to compare two date in angular to know The number of days enter image description here

2 Answers2

0

I'd not use a date library in order to do a particular corner case but nowadays people normally use date-fns in order to work with dates.

So if you use that it's really simple basically you can compare dates by doing:

isEqual(date1, date2)

isEqual doc

Get days difference between two dates

Get number of days in a year

Get number of days in a month

And so on... the lib has tons of methods to work with days, dates, etc..

Have a look on the links above, you can basically do anything you want with dates in a really simple way. Also, if you gonna work using timezones you can use date-fns-tz.

Pedro Bezanilla
  • 849
  • 10
  • 20
  • i want to know number of date not compare – saadouna May 19 '22 at 09:08
  • calculateDiff() { var date1 = this.date_debut; var date2 = this.date_fin; console.log("date1", date1) console.log("didate2", date2) this.nbj = (this.datef - this.dated) / 86400000; console.log("nbjr", this.nbj) return this.nbj } – saadouna May 19 '22 at 09:09
  • @saadouna https://date-fns.org/v2.28.0/docs/differenceInDays if you search for 'difference' on that page you can find tons of alternatives for that case. – Pedro Bezanilla May 19 '22 at 09:15
-1

this is my code

enter code here
onBlurEvent(event: any){
  console.log(event.target.value);
  this.date_debut = event.target.value;
  console.log("date_debut",this.date_debut)
 }
 onEvent(event: any){
  console.log(event.target.value);
  this.date_fin = event.target.value;
  console.log("date_fin",this.date_fin)
 }

 calculateDiff(date1:any,date2:any) {
  var date1 = this.date_debut;
  var date2 = this.date_fin;
  var diffDays:any = Math.floor((date2 - date1) / (1000 * 60 * 60 * 24));
  console.log("diffDays",diffDays)
  return diffDays;
  

}