Getting fresh… with JavaScript that is

One of my project for work this year is to port an old promise date calculator I had written in Microsoft Access to a more web friendly version. Originally I had planned on using ASP.NET for the conversion but after a few hours of research I decided using ASP.NET for such a small application is overkill and would put a lot of unnecessary CPU cycles on the server.

Such simple dates calculation are better off done on the client side via the browser. The obvious language of choice for this task is Javascript and with the ever popular jQuery ui library I can easily put together a working UI in as few as 15 minutes.

I used jQuery for the datepicker, since I am only working with business days I was able to find a CSS hack that disable weekends from the date picker.

<style type="text/css">
 /* remove weekends from popup calendar */
 th.ui-datepicker-week-end,
 td.ui-datepicker-week-end {
 display: none;
 }
</style>
jQuery date picker

jQuery date picker

The premise of the date calculator is very simple. Given  a start date, date unit was put on hold and the original promise date. The calculator should programatically determine

  1. how many business days have passed between the date unit was put on hold and the start date(normally today)
  2. the # of holidays that occurred between the 2 dates span
  3. and finally add the # of business days to the original promise date to get the new promise date.

The application should also verify that the new promise date itself is not a holiday and make adjustments as necessary.

I am very grateful to Lucas Smith for his elegant addBusinessDays function. This is far more efficient that what I came up with.

function addBusinessDays(d,n) {
 d = new Date(d.getTime());
var day = d.getDay();
d.setDate(
 d.getDate() + n +
 (day === 6 ? 2 : +!day) +
 (Math.floor((n - 1 + (day % 6 || 1)) / 5) * 2));
return d;
}

Source: http://lucassmith.name/2008/11/add-business-days-to-a-date.html

After a few days of work I was able to put together a functional calculator. I spent another 2 days fixing a bug in Internet Explorer 8. This is the current deployed version. Its much more faster and convenient than the Microsoft Access behemoth I had everyone using before.

Promise Date Calculator

Currently the 9 holidays are manually entered in an array and will need to be updated each year for the calculator to work. My todo list for this project is to dynamically calculate the 9 holidays so the application can be self contained without needing someone to do the yearly maintenance  to update the holidays.

Download  source code via CodeProject http://www.codeproject.com/Articles/455070/Promise-Date-Calculator

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s