You are viewing limited content. For full access, please sign in.

Question

Question

Javascript for PTO form

asked on July 30, 2015

I am building a PTO form and need to add up the number of hours of PTO requested and also sort by day.  The issue is I need to automatically remove weekend days.  For example.  If I request PTO from July 31-August 4th.  That should be 3 days x 8 hours or 24 hours, not 5 days x 8 hours or 40 hours.  Is there a way to have javascript recognize days that are weekends and automatically delete them?

 

My thought is that the script would see 5 days (40 Hours), then see that there are two days that are weekend days (16 hours).  That way when it generates the email for approval it only shows 16 hours.  Also, when it gets to Laserfiche, I can use metadata to create an exchange task that adds those days to a corporate vacation calendar. 

0 0

Answer

APPROVED ANSWER
replied on July 30, 2015 Show version history

You can use moment.js, but you'll still have to perform the calculation yourself. You can use something like

$(document).ready(function () {
  
  $.getScript('http://server/Forms/js/moment.js');

  $('.numdays input').attr('readonly',true);
  $('.hours input').attr('readonly',true);
  
  $('.startdate input, .enddate input').on('change', function () {
    var start = moment($('.startdate input').val());
    var end = moment($('.enddate input').val());
    $('.numdays input').val(businessday_count(start,end));
    $('.hours input').val(businessday_count(start,end)*8);
    
  });

  function businessday_count(start,end) { 
    var daysdiff = end.diff(start,'days');
    var weeksdiff = end.diff(start,'weeks');
    if (start.day() > end.day()) {
      weeksdiff += 1;
    }
    var weekenddays = weeksdiff*2;
    if (end.day() == 6) {
      return parseNumber(daysdiff - weekenddays);
    } else {
      return parseNumber(daysdiff - weekenddays + 1);
    }
  }
  
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
  
});

The form has two date fields with the CSS class startdate and enddate. There is also a single line field with the CSS class numdays and another single line field with the CSS class hours.

0 0

Replies

replied on July 30, 2015

Have you tried adding in Moment.js and then using that to perform calculations? There is a lot of logic that you can completely avoid having to do yourself.

0 0
replied on July 30, 2015

I assume this is where to get that script?  http://momentjs.com/

0 0
replied on July 30, 2015

Yea, you can use the .diff function to figure out the # of days between. I cannot remember where the function to limit it to weekdays is though, I thought I saw that in that library. But you can also use Moment.js to add a day to the start date and see what that day is, and if its a weekend and still before the end date, you subtract it from your difference. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.