Jump to content
Sign in to follow this  
Emari

JS to limit datepicker

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);

Share this post


Link to post
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) {

 

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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...?

Share this post


Link to post
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

Share this post


Link to post
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...?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...