Thursday, 4 June 2020

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

 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
            }
        }

function quarter_of_the_year(date) 
  {
    var month = date.getMonth() + 1;
    return (Math.ceil(month / 3));
  }

No comments:

Post a Comment