Skip to content

Instantly share code, notes, and snippets.

@andrewjkerr
Last active December 20, 2015 01:39
Show Gist options
  • Save andrewjkerr/6050428 to your computer and use it in GitHub Desktop.
Save andrewjkerr/6050428 to your computer and use it in GitHub Desktop.
Uses the jQueryUI datepicker and the JavaScript date object to calculate the number of business days remaining between today and whatever date is picked with the datepicker.
$(function() {
$( "#datepicker" ).datepicker({
onSelect: function(){
// Gets the information from the picked date
var picked = $("#datepicker").datepicker('getDate');
var ddPicked = $("#datepicker").datepicker('getDate').getDate();
var mmPicked = $("#datepicker").datepicker('getDate').getMonth();
var yyyyPicked = $("#datepicker").datepicker('getDate').getFullYear();
var dotwPicked = $("#datepicker").datepicker('getDate').getDay();
// Set up today's date
var today = new Date();
var date = today;
var remaining = 0;
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
var dotw = today.getDay();
// Checks for whether the date has already occured
if(picked < today){
alert("Yo dawg, that date has already passed.");
remaining = 0;
}
else{
// Loops through dates
while(date < picked){
// Sets up the date that the loop is currently at
var dd = date.getDate();
var mm = date.getMonth();
var yyyy = date.getFullYear();
var dotw = date.getDay();
// Adds a day to remaining days if the day isn't a Saturday or a Sunday
if(dotw != 0 && dotw != 6){
remaining++;
}
dd++;
// Checks for whether the month is a February
if(mm == 1){
// Checks for leap year
if(yyyy % 4 == 0){
// If so, there are 29 days in February and goes to the next month
if(dd == 29){
mm++;
dd = 0;
}
}
// If not, there are 28 days in February and goes to the next month
else{
if(dd == 28){
mm++;
dd = 0;
}
}
}
// If the month has 30 days in it, goes to the next month
else if(mm == 3 || mm == 5 || mm == 8 || mm == 10){
if(dd == 30){
mm++;
dd = 0;
}
}
// If the month has 31 days in it, goes to the next month
else{
if(dd == 31){
if(mm == 11){
yyyy++;
dd = 0;
mm = 0;
}
else{
mm++;
dd = 0;
}
}
}
// Set the current date of the loop
date.setFullYear(yyyy, mm, dd);
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment