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

Notifications

Announcements

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 24 Super User 2025 Season 2

#2
TAHER Mehdi Profile Picture

TAHER Mehdi 14

#3
Fameeda Yaseen Profile Picture

Fameeda Yaseen 10

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans