views:

55

answers:

1
+1  Q: 

Calendar date help

Hey all, I've been trying to figure out how to go about this problem i have encountered. I need to figure out a way to check what day was chosen on the calendar and see if anything surrounding it is un-clickable. In other words, if a date is disabled after the selected date then it can not go any farther.

Here is an example:

Say its March and March has 31 days total. The user chooses day 9. Therefore, days 1-8 are not selectable (i have already coded this part)

Now say that day 10 is not selectable since its already taken by something. In this case, the user will not be allowed to click any other date past the 9th since it can not cross over a day thats already been taken by something.

I really don't know how to check for that BUT i do have the days already in a array type that are disabled. That array would look like this:

[10,15,20]

As in the case above, i know what days are not selectable but am unsure of how to go about coding it if the user (again, per the example above) choose day 11 and that crosses over day 10 thats already not selectable.

Here is a picture of it: http://img704.imageshack.us/img704/9803/cals.jpg

Any help and ideas would be great!

Thanks, David

Ok, using ASP i have come up with a soluction to most of the problem:

 sVal = tempResults 'This is the array of used dates already
 tmpDate = tmpDate + 1 'This is the date they chose (9) and added a 1 making it 10 (next day)
 theMonthDays = getDaysInMonth(theMonth, theYear) ' This just gets how many days are in the current selected month
 ArrayOfValues = Split(sVal, ",") 'this splits the array of used dates to be checked below

 For i = 0 To UBound(ArrayOfValues)     
    if tmpDate = CInt(ArrayOfValues(i)) then 'if it finds a date used already then block the rest
        dim z, theBlockedDays

        z = tmpDate
        do until z = theMonthDays + 1
            theBlockedDays = "'" & theMonth & "-" & z & "-" & theYear & "'," & theBlockedDays
            z = z + 1
        loop
        exit for
    end if
Next

Now that works for the day 9 but does not work if i choose day 11 since the next day is open and then day after that is opened as well. How would i go about checking for that?

David

A: 

Let me see if I understand your problem clearly.

Let's say the blocked out dates are d0, d1, d2, ...dn, in ascending order.

Then when a user pick a date k which is not a blocked date, you:

  • Block out dates [1..k] (you said you already have done this part)
  • Block out dates [dj..31], where j is the lowest such that dj > k.

So how do you find dj? Well, in general, this is the kind of problem that binary search is designed for. You already have d0, d1, ...dn sorted, so binary search would find dj in O(log n). However, since we're talking about calendar dates, there are only 31 possible numbers, so searching it linearly doesn't hurt either.

Here's a simple code for you to get started with:

<script>

function search(v, a) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] > v) {
      return a[i];
    }
  }
  return Infinity;
}

alert(search(5, [10, 15, 20]));  // 10 --> block out [10..31]
alert(search(12, [10, 15, 20])); // 15 --> block out [15..31]
alert(search(17, [10, 15, 20])); // 20 --> block out [20..31]
alert(search(25, [10, 15, 20])); // Infinity --> blockout [Infinity..31], i.e. nothing

</script>
polygenelubricants
Hum, i don't think i get your example...Referring back to the picture.. This calendar is for the "end date" so whatever square is the green color is where they picked to Start.Now as i said above, if the user picked day 9 then they could not choose another ending date other than 9 since day 10 has already been taken and they are not allowed to save a date over another one (9-11 covering up 10 on the way)continues in the next post..
StealthRT
Now if the user choose day 11 then that would be green and they could choose either day 12 or 13 (making it 11-12 or 11-13) before not being able to pick anything else after that since day 14-16 is already taken. If the user choose day 17, however, then i need to be able to tell that it would be crossing over already taken dates (days 14-16).continues in the next post....
StealthRT
I know that all i would need to do is check for the day before the chosen day AND the day after it and check to see if those days are in the array but i do not really know how to code that. But thats a problem since if that were true then day 9 would come back as bad since the day before it is taken and the day after is taken.. Maybe i am just over-thinking this?David
StealthRT
Let's say the blocked out dates array `a = [1,2,3,4,5,6,7,8,10,14,15,16,24,25,26,27,28,29,30]`; this corresponds with the calendar in the picture. Then `search(9, a) == 10`; you block all dates after `10`, leaving `9` available. Also, `search(11, a) == 14`; you block all dates after `14`, leaving `11-13` are available. Also, `search(17, a) == 24`; you block all dates after `24`, leaving `17-23` available. If I understand you correctly, all of the above is what you wanted, no?
polygenelubricants
How can i post a "normal" reply instead of a comment? I need to post some code! :o)David
StealthRT
Just edit your original question.
polygenelubricants
If someone could translate the java code above then i think i can get this wrapped up! :o)function search(v, a) { for (var i = 0; i < a.length; i++) { if (a[i] > v) { return a[i]; } } return Infinity; } alert(search(5, [10, 15, 20])); // 10 --> block out [10..31]
StealthRT