web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Service | Customer Service, Contact Center, Fie...
Answered

Skip Weekends & Business Holiday

(0) ShareShare
ReportReport
Posted on by 45

Hi ,

I am looking JS code for calculate end date (from start date & No.of Days ).I have archive through JS but need to skip weekends. 

pastedimage1640081390120v1.png

function Count_Date(executionContext){
var formContext = executionContext.getFormContext();
var Approved=formContext.getAttribute("cre9e_approved").getValue();
var Days=formContext.getAttribute("cre9e_noofdays").getValue();
if (Days != null && Approved == 859640000) {
var startdate = formContext.getAttribute("cre9e_startdate").getValue();
var enddate = new Date();
enddate.setTime(startdate.getTime() + (Days * 24 * 60 * 60 * 1000));
formContext.getAttribute("cre9e_enddate").setValue(enddate );


}
}

is there any way to skip weekends after providing No of days

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi zahid.tectree,

    Please refer to this post in the Stack Overflow:

    stackoverflow.com/.../add-no-of-days-in-a-date-to-get-next-dateexcluding-weekends

  • zahid.tectree Profile Picture
    45 on at

    Hi,

    can u please provide a code because i have try many ways to archive but not working (its working in vs code & other).if there is any other way to archive please let me know want to archive (skip weekend as well as holidays).

    Thanks

  • Verified answer
    Community Member Profile Picture
    on at

    Hi zahid.tectree,

    Please try the following code:

    function onLoad(executionContext){
    	var formContext = executionContext.getFormContext();
    
    	// Get value
    	var startDate= formContext.getAttribute("cr2f9_startdate").getValue();
     	var Days=formContext.getAttribute("cr2f9_noofdays").getValue();
    	
    	//holidays array
    	var holidays = [new Date("2021/12/24").getTime()];
    	if(startDate != null && Days != null){
    		var endDate = "",count = 0;
    		while (count < Days){
    			endDate = new Date(startDate.setDate(startDate.getDate()   1));
    			if(endDate.getDay() != 0 && endDate.getDay() != 6 && !holidays.includes(endDate.getTime())){
           				count  ;
        			}
    		}
    		formContext.getAttribute("cr2f9_enddate").setValue(endDate);
    	}
    }
    

    Result:

    pastedimage1640245172424v1.gif

  • zahid.tectree Profile Picture
    45 on at

    Thanks

    Its work but I want to get holiday dynamically .

    scenario I have a custom entity name holidays where i have added some holidays. wants to get that holiday .

    is there any way ...........

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi zahid.tectree,

    You could use retrieveMultipleRecords() method to get all records in your custom entity and then add the holiday date to the holiday array.

  • zahid.tectree Profile Picture
    45 on at

    could you please provide  more information

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi zahid.tectree,

    Please refer to this blog to use retrieveMultipleRecords() method: https://carldesouza.com/retrieve-and-retrievemultiple-javascript-using-xrm-webapi/

    If you are still stuck, please provide the columns in your custom entity and I would provide the code to do this on Monday.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 30 Super User 2026 Season 1

#2
NeerajPawar Profile Picture

NeerajPawar 22

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 14

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans