Days Difference Excluding Weekends in Angular 6 | 7
This post is all about how to find days difference excluding weekends angular 6 | 7. Suppose you have two different dates and we need to find the days between dates excluding weekends in angular 6. Here you can find a simple two function to get this perform. Angular 6 is nowadays are very most used UI development framework, which could be used to develop web applications and mobile applications. We are trying to provide this kind of blog which could help developers to get this without wasting their time much.
Table of Contents
Step-1 : Put this html code in your component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<div class="form-group" style="width:200px;float: left;"> <label class="labeltext">From date : </label> <input type="date" [(value)]="fromDate" class="form-control" (change)="onKeyUpfromdate($event)" /> </div> <div class="form-group" style="width:200px;float: right; margin-left: 5px;"> <label class="labeltext" >To date : </label> <input type="date" [(value)]="toDate" class="form-control" (change)="onKeyUptoDate($event)" /> </div> </div> <div class="form-group" style="width:200px;"> <label class="labeltext">Total Days : </label> <input type="text" name="date" class="form-control" [(value)]="leaveDays" readonly /> </div> |
Step:2- Put this below function in your ts files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
onKeyUpfromdate(event: any) { this.fromDate = event.target.value; console.log(this.fromDate); this.todateSec = new Date(this.toDate); this.fromdateSec = new Date(this.fromDate); if (this.todateSec < this.fromdateSec) alert('To date must be grater that from date!'); // Calculate days between dates this.millisecondsPerDay = 86400 * 1000; // Day in milliseconds this.fromdateSec.setHours(0,0,0,1); // Start just after midnight this.todateSec.setHours(23,59,59,999); // End just before midnight this.diff = this.todateSec - this.fromdateSec; // Milliseconds between datetime objects this.days = Math.ceil(this.diff / this.millisecondsPerDay); // Subtract two weekend days for every week in between this.weeks = Math.floor(this.days / 7); this.days = this.days - (this.weeks * 2); // Handle special cases this.fromdateSec = this.fromdateSec.getDay(); this.todateSec = this.todateSec.getDay(); // Remove weekend not previously removed. if (this.fromdateSec - this.todateSec > 1) this.days = this.days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (this.fromdateSec == 0 && this.todateSec != 6) this.days = this.days - 1; // Remove end day if span ends on Saturday but starts after Sunday if (this.todateSec == 6 && this.fromdateSec != 0){ this.days = this.days - 1 ; } this.leaveDays = this.days; if(this.leaveDays =='NaN' || this.leaveDays =='' || this.leaveDays <='0' || this.leaveDays =='undefined'){ this.leaveDays =''; }else{ this.leaveDays = this.days; } } onKeyUptoDate(event: any) { this.toDate = event.target.value; console.log(this.toDate); //alert(this.toDate); //alert(this.fromDate); this.todateSec = new Date(this.toDate); this.fromdateSec = new Date(this.fromDate); if (this.todateSec < this.fromdateSec) alert('To date must be grater that from date!'); // Calculate days between dates this.millisecondsPerDay = 86400 * 1000; // Day in milliseconds this.fromdateSec.setHours(0, 0, 0, 1); // Start just after midnight this.todateSec.setHours(23, 59, 59, 999); // End just before midnight this.diff = this.todateSec - this.fromdateSec; // Milliseconds between datetime objects this.days = Math.ceil(this.diff / this.millisecondsPerDay); // Subtract two weekend days for every week in between this.weeks = Math.floor(this.days / 7); this.days = this.days - (this.weeks * 2); // Handle special cases this.fromdateSec = this.fromdateSec.getDay(); this.todateSec = this.todateSec.getDay(); // Remove weekend not previously removed. if (this.fromdateSec - this.todateSec > 1) this.days = this.days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (this.fromdateSec == 0 && this.todateSec != 6) this.days = this.days - 1; // Remove end day if span ends on Saturday but starts after Sunday if (this.todateSec === 6 && this.fromdateSec !== 0) { this.days = this.days - 1 ; } this.leaveDays = this.days; if ( this.leaveDays === 'NaN' || this.leaveDays === '' || this.leaveDays <= '0' || this.leaveDays =='undefined'){ this.leaveDays = ''; } else { this.leaveDays = this.days; } } |
Step-3: Declare the variables inside the main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
export class LeavemanagementComponent implements OnInit { leaveBalance:boolean=false; applyLeave:boolean=true; fromDate=newDate(); toDate=newDate(); days:any; todateSec:any; fromdateSec:any; millisecondsPerDay:any; diff:any; weeks:any; leaveDays=''; .... } |
Now you are done. Save and run your application. It will work fine.
If you have any query please comment us. We will get back to you with a solution.
thanks for reading how to find days difference excluding weekends angular 6 | 7.