Support
Quality
Security
License
Reuse
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
Get all kandi verified functions for this library.
lightweight, powerful javascript datetimepicker with no dependencies
QUESTION
I am trying to validate that is end time is greater then small time with jquery
Asked 2022-Apr-11 at 09:21i am laravel developer and i have no experience with jquery but i am trying to validate that is end time is greater then start time but unfortunately i am little confused my jquery code right or not please help me thanks.
eg. start time = 09:14 and end time 12:15 - valid
and start time = 09:14 and end time 01:15 - not valid
thanks have a nice day
I think a/c to this time it should be validate.i am little confused why are giving me this error end time should be greater then start time.
Html view
<div class="row my-4">
<div class="col-md-12 text-center mt-3">
<label for="" class="small text-muted">Select Time</label>
</div>
<div class="col-md-5">
<input type="time" class="form-control flatpickr" name="start_time"
id="start_time" placeholder="Start Time" >
</div>
<div class="col-md-2 d-flex justify-content-center align-items-center">
<i class='bx bx-minus'></i>
</div>
<div class="col-md-5">
<input type="time" class="form-control flatpickr" name="end_time" id="end_time" placeholder="End Time" >
<span class="endTimeMessage"></span>
</div>
</div>
jquery
$(document).change(function(){
var startTime =$('#start_time').val(); // 9:15 Am
var endTime = $('#end_time').val(); // 1:00 pm
var startTimeParts = startTime.split(":");
var endTimeParts = endTime.split(":");
var startDate = new Date();
startDate.setHours(startTimeParts[0],startTimeParts[1],0,0);
var endDate = new Date();
endDate.setHours(endTimeParts[0],endTimeParts[1],0,0);
if(endTime != ""){
if (startDate < endDate){
$(".endTimeMessage").hide();
$('#btnSubmit').removeAttr("disabled",true);
return true;
}else{
$(".endTimeMessage").show();
$(".endTimeMessage").text("End time should be greater then start time");
$('#btnSubmit').prop('disabled', true);
return false;
}
}
});
ANSWER
Answered 2022-Apr-11 at 09:21Since I see you are using Flatpickr, you can try this feature.
The totalWorkTime()
function checks if the work time is more than 0 seconds, but also not more than 24 hours.
When the start time is entered, the Flatpickr end time is updated to prevent users from entering a value lower than the start time.
For example, if a user enters the start time of 12:00 AM, they cannot enter 11:59 AM as the end time.
$(document).ready(function() {
const start_time = $('#start_time').flatpickr({
enableTime: true,
noCalendar: true,
time_24hr: true,
minuteIncrement: 30
});
const end_time = $('#end_time').flatpickr({
enableTime: true,
noCalendar: true,
time_24hr: true,
minuteIncrement: 30
});
$(document).change(function() {
let your_msg = $('.endTimeMessage');
let time_worked = totalWorkTime(start_time, end_time);
if (time_worked != null) {
your_msg.html(`You have worked ${time_worked} hours.`);
} else if (isset(time_worked)) {
your_msg.html(`End time should be greater then start time.`);
} else {
your_msg.html(`Notice: Not all fields are filled in.`);
}
});
function totalWorkTime(set_time_start, set_time_end) {
if (isset(start_time.selectedDates[0]) && isset(end_time.selectedDates[0])) {
let date_start = set_time_start.selectedDates[0].getTime();
let date_end = set_time_end.selectedDates[0].getTime();
let max_date = new Date(date_start + 60 * 60 * 24 * 1000);
if (date_start > date_end) {
// Update flatpickr to prevent value date_start to be higher then date_end
set_time_end.set('defaultDate', max_date);
}
set_time_end.set('minDate', new Date(date_start))
set_time_end.set('maxDate', max_date)
let sec = Math.floor((date_end - (date_start)) / 1000);
let min = Math.floor(sec / 60);
let hours = Math.floor(min / 60);
let days = Math.floor(hours / 24);
hours = hours - (days * 24);
min = min - (days * 24 * 60) - (hours * 60);
return (isNaN(hours) || days !== 0 || sec === 0) ? null : ('0' + hours).slice(-2) + ':' + ('0' + min).slice(-2);
}
}
function isset(data) {
return typeof data !== 'undefined';
}
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.11/flatpickr.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.11/flatpickr.min.js"></script>
<div class="container">
<div class="row justify-content-center">
<div class="col-12 text-center my-3">
<label for="" class="small text-muted">Select Time</label>
</div>
<div class="col-auto">
<input type="time" class="form-control flatpickr" name="start_time" id="start_time" placeholder="Start Time">
</div>
<div class="col-auto d-flex justify-content-center align-items-center">
<i class='bx bx-minus'></i>
</div>
<div class="col-auto">
<input type="time" class="form-control flatpickr" name="end_time" id="end_time" placeholder="End Time">
</div>
<div class="row my-4 text-center"><span class="endTimeMessage"></span></div>
</div>
</div>
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
See Similar Libraries in
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source