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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamically setting start date and end date based on quarters using javascript

(0) ShareShare
ReportReport
Posted on by 195

Hi this is Bharath. I have 1 option set Field(Period).It has Q1,Q2,Q3,Q4. I wanna set start date and end date based on the quarter selected. I have code which is hard coded.i should not hard code i should set dynamic values.

for example Q1 starts at july 1st and ends at september 30.

Q2 October 1st and ends at 31st december,

Q3 Jan 1st and ends at march 31st.

Q4 April 1st and ends at june 30.

Thanks,

Bharath

*This post is locked for comments

I have the same question (0)
  • Justinjose Profile Picture
    2,707 on at
    RE: Dynamically setting start date and end date based on quarters using javascript

    Hi Bharath,

    your question is bit confusing. Can you please explain in detail.

    1. Q1 one will be always 1 July?

    2. Why are you preferring Dynamic values?

    Thanks

    Justin Jose

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Dynamically setting start date and end date based on quarters using javascript

    Hi Bharath,

    Try with this - Need to fire the code in onchange method of Quarters optionset.

            function QuarterOnChange() {
                var startDate = null;
                var endDate = null;
                //Get Year for next year if current month is July , you can change as per your Business rules.
                var todaydate = new Date();
                var currentYr = todaydate.getFullYear();
                var CurrentMonth = (todaydate.getMonth() + 1);
                var currentDate = todaydate.getDate();
    
                var nextQtrYr = "";
                if (CurrentMonth > 6) {
                    nextQtrYr = currentYr + 1;
                }
                else {
                    nextQtrYr = currentYr;
                }
                //
    
                var selQtr = Xrm.Page.getAttribute("new_qarter").getSelectedOption().value; // Replace optionset field Name
                if (selQtr != null & selQtr != undefined) {
                    if (selQtr == "1") {
                        //Q1 starts at july 1st and ends at september 30.
                        startDate = new Date("07/01/" + nextQtrYr);
                        endDate = new Date("09/30/" + nextQtrYr);
                    }
                    else if (selQtr == "2") {
                        //Q2 October 1st and ends at 31st december
                        startDate = new Date("10/01/" + nextQtrYr);
                        endDate = new Date("12/31/" + nextQtrYr);
    
                    }
                    else if (selQtr == "3") {
                        // Q3 Jan 1st and ends at march 31st.
                        startDate = new Date("01/01/" + nextQtrYr);
                        endDate = new Date("03/31/" + nextQtrYr);
    
    
                    } else if (selQtr == "4") {
                        //Q4 April 1st and ends at june 30.
                        startDate = new Date("04/01/" + nextQtrYr);
                        endDate = new Date("06/30/" + nextQtrYr);
                    }
                    Xrm.Page.getAttribute("new_startDate").setValue(startDate);  // Replace Field Name
                    Xrm.Page.getAttribute("new_endDate").setValue(endDate); // Replace Field Name
                }
            }


    I think similar thing you can achieve using OOB Calculated Field instead of writing code, try once if you can do this using calculated field.

  • bharrath Profile Picture
    195 on at
    RE: Dynamically setting start date and end date based on quarters using javascript

    thanks gowtham.

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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans