Get all dates between two dates retrieved from a string and make a string

I have a string like the following:

#CI[20131229]CO[20131231]PV1,#CI[20130918]CO[20130919]PV3,#CI[20131009]CO[20131011]PV3,#CI[20131226]CO[20140103]PV4,#CI[20131226]CO[20140103]PV3

.... list goes on with same format. Format is:

,# = entry seperator
CI[YYYYMMDD] = Check In Date
CO[YYYYMMDD] = Check Out Date
PVx = Room Number

I want to get all dates within the check in and checkout EXCEPT the checkin checkout date and put to string like this (if it was the above string):

#20131230PV1,#20131010PV3,#20131227PV3,#20131228PV3,#20131229PV3,#20131230PV3,#20131231PV3,#20140101PV3,#20140102PV3

I needed to retrieve CheckIn dates and Checkout dates seperately That I managed to did successfully with this:

bookcaldatesci.innerHTML = bookcaldates.innerHTML.replace(/CO[^\]]+\]/ig, "");
bookcaldatesci.innerHTML = bookcaldatesci.innerHTML.replace(/CI\[/ig, "");
bookcaldatesci.innerHTML = bookcaldatesci.innerHTML.replace(/\]/ig, "");
bookcaldatesco.innerHTML = bookcaldates.innerHTML.replace(/CI[^\]]+\]/g, "");
bookcaldatesco.innerHTML = bookcaldatesco.innerHTML.replace(/CO\[/ig, "");
bookcaldatesco.innerHTML = bookcaldatesco.innerHTML.replace(/\]/ig, "");
jQuery.noConflict();
$(bookcaldatesci.innerHTML).css({'background-image':'url("img/booked.jpg")','background-repeat':'no-repeat','background-position-y':'100%','background-size':'100% 50%'});
$(bookcaldatesco.innerHTML).css({'background-image':'url("img/booked.jpg")','background-repeat':'no-repeat','background-size':'100% 50%'});

But to add up dates between the date range I'm a bit lost..

(I GOT THE ANSWER: TO CHECK, CHECK MY ANSWER BELOW)

Upvotes: 1

Views: 1621

Answers (3)

After several hours of trying I figured the answer. It will produce the following result:

#30122013PV1,#10102013PV1,#27122013PV1,#28122013PV1,#29122013PV1,#30122013PV1,#31122013PV1,#112014PV1,#212014PV1,#27122013PV1,#28122013PV1,#29122013PV1,#30122013PV1,#31122013PV1,#112014PV1,#212014PV1,

This is the code

<div id=oo></div>
<div id=pp>#CI[20131229]CO[20131231]PV1,#CI[20130918]CO[20130919]PV3,#CI[20131009]CO[20131011]PV3,#CI[20131226]CO[20140103]PV4,#CI[20131226]CO[20140103]PV3</div>

<script>

function daysBetween(startDate, endDate , roomNum) {
var days = [], current = startDate;
current.setDate(startDate.getDate()+1);
endDate.setDate(endDate.getDate()-1);
while(current <= endDate) {
var d0 = new Date(current);
var d1 = d0.getDate();
var d2 = d0.getMonth()+1
var d3 = d0.getFullYear()
var d4 = roomNum
oo.innerHTML = oo.innerHTML + "#" + d1 + "" + d2 + "" + d3 + "" + d4 + ",";
days.push(current);
    current = new Date(current.getTime() + (24 * 60 * 60 * 1000));   

}
return days + "," + roomNum;
}

function parseDate(date) {
return new Date(date.substring(0,4), date.substring(4,6)-1, date.substring(6,8));
}


var input = pp.innerHTML;
var regExp  = /CI\[(\d{8})\]CO\[(\d{8})\](PV\d)/g;
var items = [];
var match = regExp.exec(input);
while(match != null) {
items.push({
    ci : parseDate(match[1]),
    co : parseDate(match[2]),
    room : match[3]
});

match = regExp.exec(input);

}

for (var i = 0; i < items.length; i++) {
daysBetween(new Date(items[i]['ci']), new Date(items[i]['co']), items[0]['room']);
}


</script>

Upvotes: 0

josketres
josketres

Reputation: 3369

You can decompose the problem into two sub-problems:

  1. Parsing your string into an object with two dates and a room number.
  2. Finding the days between two given dates.

For the first sub-problem you can use regular expressions and the Date object constructor.

function parseDate(date) {
    return new Date(date.substring(0,4), date.substring(4,6)-1, date.substring(6,8));
}

var input = '#CI[20131229]CO[20131231]PV1,#CI[20130918]CO[20130919]PV3,#CI[20131009]CO[20131011]PV3,#CI[20131226]CO[20140103]PV4,#CI[20131226]CO[20140103]PV3';

var regExp  = /CI\[(\d{8})\]CO\[(\d{8})\](PV\d)/g;
var items = [];
var match = regExp.exec(input);
while(match != null) {
    items.push({
        ci : parseDate(match[1]),
        co : parseDate(match[2]),
        room : match[3]
    });
    match = regExp.exec(input);
}

For the second sub-problem you can use a loop to generate all the days between two dates:

function daysBetween(startDate, endDate) {
    var days = [], current = startDate;
    while(current <= endDate) {
        days.push(current);
        current = new Date(current.getTime() + (24 * 60 * 60 * 1000));
    }
    return days;
}

Upvotes: 2

Daniel
Daniel

Reputation: 56

You'll need to do a date difference between each check in and check out and do a for loop using that value. So for 1 to that value, and just add the for value to the check in date for the whole loop. Easiest way to navigate each set of check in check out dates may be either using regex or a loop based on finding string values.

Upvotes: 1

Related Questions