Functiones Beds allocation

by Felipe Alfaro

HTML

01 02 03 04 05 06 07 08 09 10
PA PA PA PA PR FU FU FU FU FU

JavaScript

// agregar dias a fecha
Date.prototype.addDays = function(days){
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
}
// string a fecha
function stringToDate(str){
	str = str.split('/').map(Number);
	return new Date(str[2],  str[1]-1, str[0]);
};
// fechas de ejemplo
var clientData = {
	today: '15/11/2016'
};
var receivedData = {
	initialDate: '1/12/2016',
    finalDate: '10/12/2016'
};
// fechas a tiempo en ms
var processedData = {
	today: stringToDate(clientData.today).getTime() / 86400000,
	initialDate: stringToDate(receivedData.initialDate).getTime() / 86400000,
    finalDate: stringToDate(receivedData.finalDate).getTime() / 86400000,
    days: function(){
    	return this.finalDate - this.initialDate + 1;
    }
};

////////////////////////////
var tiempoDia = 86400000,
	tiempoHoy = stringToDate(clientData.today).getTime(),
	tiempoInicio = stringToDate(receivedData.initialDate).getTime(),
    tiempoFinal = stringToDate(receivedData.finalDate).getTime();
var cantidadDias = ( ( tiempoFinal - tiempoInicio ) / tiempoDia ) + 1,
	cantidadDiasPasados = Math.max(0, Math.min(cantidadDias, (tiempoHoy-tiempoInicio)/tiempoDia )),
    cantidadDiasPresentesYFuturos = Math.max(0, Math.min(cantidadDias, ((tiempoFinal-tiempoHoy)/tiempoDia)+1 ));
console.log( 'total: '+cantidadDias );
console.log( 'no editables: '+cantidadDiasPasados );
console.log( 'editables: '+cantidadDiasPresentesYFuturos );
console.log('----------');
////////////////////////////

var fixeoSinNegativo = processedData.today - processedData.initialDate;


var firstPosition = processedData.initialDate - processedData.today,
	secondPosition = processedData.finalDate - processedData.today,
    ranges = [];
    
var past_1 = firstPosition + fixeoSinNegativo,
    past_2 = -1 + fixeoSinNegativo,
    presentFuture_1 = fixeoSinNegativo,
    presentFuture_2 = secondPosition + fixeoSinNegativo;

if( firstPosition < 0 ){
	
    if( secondPosition < 0 ){
   ...