Im using the car rental plugin and need to modify it in a way that if the rental time chosen by customer is less than 2 hours, to give him a message, pop up or any kind of message, that he needs to choose time minimum 2 hours for rental.
You can see the example here: http://envato.bestsoftinc.net/wp-car/
I need to make sure that there is at least 2 hour difference between pick up date field and drop off date field, if not, I need to show him message and not let him click on the Search Button. Any ideas how I can achieve that with jQuery or Regular Javascript please?
Thank you
this is the basic logic for it, try implement this with your site.
i found moment.js is really helpful with js time date obj you can give it a try
if($('#checkInDate').value() === $('#checkOutDate').value) [
if both date is the same date, than
var checkInTime = $('#checkInTime').value();
var checkOutTime = $('#checkInTime').valeu();
get time value
if(checkOutTime > checkInTime) {
checkOutTime must be later than checkInTime when it's the same date
if(checkOutTime - checkInTime > 2) {
if duration is more than 2 than this value is ok
alert('ok');
this fail 3rd if statement
} else { alert('error must less than 2 '); }
this fail 2nd if statement
} else {alert('error checkout must bigger than checkin'); }
end 1st if statement that check for same date
}
Related
I am building a reminder bot with Discord.js, and I'm trying to determine whether a user's time input matches local input.
Here are the snippets of my code
Machine Time
const checkTime = () => {
machineTime = new Date() - 0;
console.log(machineTime);
if (inputDate <= machineTime && inputDate != null && machineTime != null) {
console.log("Match found");
}
};
setInterval(checkTime, 1000);
User Input
inputDate = new Date(content[1].replace('-', ' ')) - 0;
Currently, I have a setInterval loop running and creating a new date/time (current time). I then have my code read for user input, ex: "02/2/2023-07:41:30 PM". The input is added to an array, where I pull the time index. I convert both times into timestamps, and use an if statement to determine whether those times match.
However, I'm assuming my user input time is incorrect, as it currently doesnt consider the AM/PM addition. When I try to add in the AM/PM to timestamp conversion, I get an error. Does anyone have an idea of how I can better compare times / fix my issue? Thanks
I am intending to use a datapicker that does not allow the user to choose previous days before today, but I do want that today itself is available. I did this:
<input
name = "availabilityFrom"
onChange = {(e) => handleDateIn (e.target.value, e.target.name)}
type = "date"
/>
I am working with React so this above is part of a component, that has a state. Then with the value taken, I stored it on a variable to use its valueOf (), and created today´s variable also. Please notice that this.state.filterBy.availabilityFrom holds the value of the target, the selected date on the datapicker.
let today = new Date().valueOf();
let availabilityFromToDate = new Date(this.state.filterBy.availabilityFrom.split("-").join(",")).valueOf();
Then I used a conditional statement to get the desired behavior of the alert:
if (availabilityFromToDate <today && availabilityFromToDate)
{
sweetAlert ("Warning!",
"The entry date must be after the current date";
"warning");
}
It works well, I got so happy when it did. But if I choose today's date, I got the alert message anyway, even if I am not using <= for today.
Maybe I am not understanding the proper use of valueOf (). Been having nightmares about this, haha.
Thanks in advance!: D
In the FullCalendar plugin, I need allow selection of days until a day or between dates. I put an example to explain better.
https://codepen.io/stefanmalex/pen/Jjjjgmp
I have an array with disallowed days:
var disallowedDays = ['2019-10-17', '2019-10-23', '2019-10-26']
I added the 'selectAllow' callback:
selectAllow: function (selectInfo) {
if (disallowedDays.includes(selectInfo.startStr)) {
return false;
}
return true;
}
This works perfectly if you select day per day, allows selection of all days less disallowed days in array.
PROBLEM: When you select multiple days, it allows select disallowed days. (Example: select from '2019-10-15' to '2019-10-26').
What I need, example:
If the selection starts on '2019-10-11', it has to allows you to select until '2019-10-16' because next day ('2019-10-17') is disallowed.
I let the example on codepen. https://codepen.io/stefanmalex/pen/Jjjjgmp
ADyson has recognized it correctly.
The program logic needs to be changed.
In the selectAllow you were checking the array with startStr, so basically it will be checking with start date of selection only, not the whole selection.
So, if you tried to select 14 oct to 18 oct, you needed to check / compare the disallowed dates with in this range.
So, it is needed to loop through the disallowedDays array to check each date within the tried selection, like the following loop:
for(var i=0;i<disallowedDays.length;i++) {
var dd = new Date(disallowedDays[i]);
if(dd.getTime() >= startDate.getTime() && dd.getTime() <= endDate.getTime()){
return true;
}
}
Following this logic, check here the solution you might be expecting
I have a input field which I want to fill with date and time in format yy-mm-dd hh-mm-ss, because I'm sending this information to my databases column with DATETIME (or similar) data type. I made this work with two inputs - one textfield I filled with datepicker() and other was <select> list with predefined values for time. Today I was coding another functionality in php and I didn`t like my situation with date and time, so I made javascript code like this:
<script type="text/javascript">
$(".datepicker").click(function(){
var a = "yy-mm-dd ";
var b = prompt("Ievadi laiku formātā hh-mm-ss", "00-00-00");
var c = a.concat(b);
$(".datepicker").datepicker({dateFormat: c});
});
</script>
So when I click on the input field I get a prompt where I type time and press enter. This is when I'd like to choose a date from the calendar but as datepicker actually works at the same time when prompt shows up (on click), then argument c doesn't exist at this time and calendar doesn't show up because dateFormat is invalid. If I click once again on the input field, I get another prompt and after the second prompt calendar shows up, but datepicker uses the format I was trying to set the first time not now. So if I entered "00-00-00" for the first time and "00-10-00" for the second, than after choosing the date I get "mydate 00-00-00" and not the actual time I entered this time. I've seen similar posts here but it didn't help me. There was a post of getting current time and appending to the date but I guess this is different. Should I use some other method to enter the time and then add it to date as I was trying to do it or is there a way to give my variable c a value before datepicker works? I`ll appreciate your suggestions.
<script type="text/javascript">
$(".datepicker").click(function(){
var a = "yy-mm-dd ";
var b = prompt("Ievadi laiku formātā hh-mm-ss", "00-00-00");
var c = a.concat(b);
if (c.length > 0){
$(".datepicker").datepicker({dateFormat: c});
}});
</script>
If statement solved this one. But it doesnt work every time. Ill check this tomorrow.
EDITED: this works - https://jsfiddle.net/gne64yd5/20/
I have googled a bit and could not find an answer. So here is my situation.
I have an input of type dateTime. I want to compare the value picked (mobile app for blackberry) to the current date and time. if the selected date is in the future (bigger than date now) I have to show a simple error message. This is all done when the user tries to save the data.
I have tried code like this, but was unsucessfull.
var dateOfIncident = $('#AccidentDetailsDate').val();
var dateNow = Date.now();
if(dateNow > dateOfIncident)
{
// do my stuffs :)
}
This does not work... It passes that validation. I am very new to javascript myself. Any help would be greatly appreciated. I googled and could not find a solution that does not use anything fancy. I need to do it in javascript.
Thanks in advance.
Try this:
var dateOfIncident = new Date($('#AccidentDetailsDate').val()); // or Date.parse(...)
var dateNow = new Date(); // or Date.now()
if(dateNow > dateOfIncident)
{
// do your stuffs...
}
However, if this works may depend on what format your date-string is! You may want to consider this post as well.