locked
How to calculate date difference in ms crm 2015 (only with workingdays & should not consider holiday list)? RRS feed

  • Question

  • Hi Experts,

    below is my requirement.

    There is a form with start and end date, I  need to get the date difference between those , only I need to consider working days and should not include holidays.

    Is there any OOB feature exist to track holiday list or I need to go for custom entity to track the same?

    if OOB entity exists how can I use that in JavaScript?

    Please suggest.

    Thanks

    Baji Rahaman

    Wednesday, November 18, 2015 4:05 PM

All replies

  • Hello,

    There is no such OOB entities that contain dates of holidays. You will have to create it.

    After creation of entity you will have possibility to use it inside code.


    Dynamics CRM MVP
    My blog


    Wednesday, November 18, 2015 4:22 PM
    Moderator
  • Put this function on load of form and call it on chnage of both the date fields:-

    function workingDaysBetweenDates(startDate, endDate) { // Validate input if (endDate < startDate) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDate.setHours(0,0,0,1); // Start just after midnight endDate.setHours(23,59,59,999); // End just before midnight var diff = endDate - startDate; // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); days = days - (weeks * 2); // Handle special cases var startDay = startDate.getDay(); var endDay = endDate.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay == 0 && endDay != 6) days = days - 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay == 6 && startDay != 0) days = days - 1 alert(days); }

    It will remove weekend. If you want to include bank holidays then you need to query business holiday enitity between start and end date and subtract that count from business days



    Regards Faisal


    • Edited by Faisal Fiaz Thursday, November 26, 2015 4:46 PM
    Thursday, November 26, 2015 4:43 PM