UNPKG

datility

Version:

Missing javascript Date object utilities

2 lines (1 loc) 964 B
Date.prototype.isWorkingDay=function(){return!(this.toLocaleDateString("en-US",{weekday:"long"})==="Saturday"||this.toLocaleDateString("en-US",{weekday:"long"})==="Sunday")};Date.prototype.isWeekend=function(){return this.toLocaleDateString("en-US",{weekday:"long"})==="Saturday"||this.toLocaleDateString("en-US",{weekday:"long"})==="Sunday"};Date.prototype.addWorkingDays=function(t){const n=this.isWeekend();if(isNaN(t))return new Date(NaN);const i=this.getHours(),e=t<0?-1:1,s=Math.floor(t/5);this.setDate(this.getDate()+s*7);let a=Math.abs(t%5);for(;a>0;)this.setDate(this.getDate()+e),this.isWorkingDay()&&(a-=1);return n&&this.isWeekend()&&t!==0&&(this.toLocaleDateString("en-US",{weekday:"long"})==="Saturday"&&this.setDate(this.getDate()+(e<0?2:-1)),this.toLocaleDateString("en-US",{weekday:"long"})==="Sunday"&&this.setDate(this.getDate()+(e<0?1:-2))),this.setHours(i),this};Date.prototype.subtractWorkingDays=function(t){return this.addWorkingDays(-t)};