Reputation: 2215
i m getting like startTime 13:30:00 endTime 14:15:00 (24 hour format) by dragging on html table cell. In hiddenfield i am storing values starttime and endtime
var hidStartTime=13:30:00;
var hidEndTime=14:15:00;
i have to check if users startime and endtime is past compare to current client machine time then have to generate alert("You can not allow to fix appointment for past time."); How do i check
var todaysDate = new Date();
var currentHour = todaysDate.getHours();
var currentMinutes = todaysDate.getMinutes();
if (currentHour < endTimeHour)
{
alert("You can not allow to fix appointment for past time.");
return false;
}
Upvotes: 0
Views: 216
Reputation: 4389
Assuming that the date is going to be today, you can create Date objects with the start and end times, and then compare them with the current date, like so.
var currDate = new Date();
var startDate = setTime(hidStartTime);
var endDate = setTime(hidEndTime);
// given an input string of format "hh:mm:ss", returns a date object with
// the same day as today, but the given time.
function setTime(timeStr) {
var dateObj = new Date(); // assuming date is today
var timeArr = timeStr.split(':'); // to access hour/minute/second
var hour = timeArr[0];
var minute = timeArr[1];
var second = timeArr[2];
dateObj.setHours(hour);
dateObj.setMinutes(minute);
dateObj.setSeconds(second);
return dateObj;
}
// now we can subtract them (subtracting two Date objects gives you their
// difference in milliseconds)
if (currDate - startDate < 0 || currDate - endDate < 0) {
alert("Unfortunately, you can't schedule a meeting in the past.
We apologize for the inconvenience.");
}
Upvotes: 2
Reputation: 986
You can compare 2 Date objects like this:
// Get current date on the client machine
var currentDateTime = new Date(); // "Thu, 05 Jul 2012 08:05:57 GMT"
// Now if you have a date string of 8:30 on the same day
var endDateTime = Date.parse("Thu, 05 Jul 2012 08:30:00 GMT");
if (currentDateTime > endDateTime) {
alert("...");
}
The tough part might be to compose the date string to a format which can be understood by the function Date.parse()
Upvotes: 0