Emari Posted March 20, 2020 Share Posted March 20, 2020 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 More sharing options...
Emari Posted March 26, 2020 Author Share Posted March 26, 2020 Anyone? 😟 Link to comment Share on other sites More sharing options...
Ingolme Posted March 26, 2020 Share Posted March 26, 2020 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 More sharing options...
Emari Posted March 27, 2020 Author Share Posted March 27, 2020 (edited) 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 March 27, 2020 by Emari Link to comment Share on other sites More sharing options...
Ingolme Posted March 27, 2020 Share Posted March 27, 2020 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 More sharing options...
Emari Posted March 27, 2020 Author Share Posted March 27, 2020 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 More sharing options...
Ingolme Posted March 27, 2020 Share Posted March 27, 2020 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); 1 Link to comment Share on other sites More sharing options...
Emari Posted March 31, 2020 Author Share Posted March 31, 2020 This is exactly what I was looking for, thank you very much!!!😍 Link to comment Share on other sites More sharing options...
Emari Posted April 1, 2020 Author Share Posted April 1, 2020 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now