Jump to content

JS to limit datepicker


Emari

Recommended Posts

I need help... I have a datepicker where you are only allowed to choose weekends, the problem is that after noon on the thursday you may no longer choose the upcoming saturday or sunday. The script i have below with 36 hour check works for saturday but you can still choose sunday. Can anyone help me with this?

var chosenDate = this.val();
var minDate = new Date();
minDate.setHours(minDate.getHours()+36);
if(Date.parse(chosenDate) < Date.parse(minDate)){
   this.valid(false,"error message");
   return;
}

if (this.val().getDay() !== 0 && this.val().getDay() != 6 ) {
  this.valid(false,"error message");
  return;
}
this.valid(true);

Link to comment
Share on other sites

It seems like Date.parse() returns the timestamp in GMT and so does the Date() constructor, which results in it selecting the previous day. Specifying hours seems to solve the issue. You can omit Date.parse() and just use the date objects themselves for comparison.

var chosenDate = new Date(this.val() + " 00:00:00");
var minDate = new Date();
minDate.setHours(minDate.getHours()+36);
if(chosenDate < minDate) {

 

Link to comment
Share on other sites

Yes, but 36 hours don't remove the option to choose sunday after 12.00 on thursday because there are more than 36 h between thursday and sunday. So I need another way to stop sunday from being available from 12.00 on thursday... I can't add more hours than 36 because then saturday won't be available... 🥴

Edited by Emari
Link to comment
Share on other sites

I thought you did not want Saturday to be available after noon on Thursday. That would mean you have to extend the minimum permitted date to 60 hours from the current time.

Link to comment
Share on other sites

I don't want saturday OR sunday to be available after noon on thursday. If I set it to 60 h then saturday will not be available before noon on thursday. It's hard to explain...
For saturday there can be a time limit of 36 hours and for sunday there must be a time limit of 60 hours. How do I specify that with js? Two different time limits...?

Link to comment
Share on other sites

I see. You will need some special logic to run between Thursday and Saturday. It will use 60 hours but only after 12pm on Thursday.

var THURSDAY = 4;
var minDate = new Date();
var offset = 36;
if(minDate.getDay() > THURSDAY || minDate.getDay() == THURSDAY && minDate.getHours() >= 12) {
  // Add 60 hours instead of 36 after noon on Thursday
  offset = 60;
}
minDate.setHours(minDate.getHours() + offset);

 

  • Thanks 1
Link to comment
Share on other sites

I thought I could use the same script for weekdays but it seems like I'm doing something wrong. I want it to stop from choosing the same day and if time is after noon also stop from choosing the next day:


var chosenDate = this.val();
var minDate = new Date();
var offset = +1;
if(minDate.getDay() == chosenDate.getDay && minDate.getHours() >= 12) {
  // Add 1 day after noon
  offset = +2;
}
minDate.setDate(minDate.getDate() + offset);
if(Date.parse(chosenDate) < Date.parse(minDate)){
    this.valid(false,"error message");
    return;
}
else {
   this.valid(true);
}

 

Help again...?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...