I have a textbox where I get the time as HH:MM AM/PM(like 5:30 PM). I want to validate the time whether it is 1 hour greater than the current time or not. How can I do this using javascript?
+1
A:
First of all, I'd suggest using an existing date-handling library like Datejs. You will have the benefit if a time-tested library with a comprehensive set of methods.
With Datejs, your answer is as simple as:
Date.parse(timeText).isAfter((new Date()).addHours(1));
If you'd rather not use Datejs, you can use regular expressions to parse the time before validating it. I've written a set of functions, along with unit tests which run in the Firebug console, that do just that:
function isOneHourGreater(timeText, time) {
var parsedTime = parseTime(timeText);
if (parsedTime === null) { return null; }
return (parsedTime.getTime() - time.getTime()) > (1 * 60 * 60 * 1000);
}
function parseTime(timeText) {
timeText = timeText.replace(/^\s+|\s$/g, '');
var regex = /^(\d{1,2}):(\d{1,2})\s*(am|pm)$/i;
if (!regex.test(timeText)) { return null; }
var
timeParts = regex.exec(timeText)
, hours = parseInt(timeParts[1])
, ampm = timeParts[3].toLowerCase()
, hoursOffset = (ampm === 'pm' && hours !== 12) ? 12 : 0;
hoursOffset = (ampm === 'am' && hours === 12) ? -12 : hoursOffset;
return getTodayWithTime(
hours + hoursOffset
, parseInt(timeParts[2])
);
}
function getTodayWithTime(hours, minutes) {
var today = new Date();
today.setHours(hours);
today.setMinutes(minutes);
today.setSeconds(0);
today.setMilliseconds(0);
return today;
}
function parseTimeTests() {
var now = new Date();
var tests = [
{target: "12:00 am", expected: getTodayWithTime(0, 00)}
, {target: "12:01 am", expected: getTodayWithTime(0, 01)}
, {target: "12:00 pm", expected: getTodayWithTime(12, 00)}
, {target: "12:01 pm", expected: getTodayWithTime(12, 01)}
, {target: "1:30 Pm", expected: getTodayWithTime(13, 30)}
, {target: "10:59 Pm", expected: getTodayWithTime(22, 59)}
, {target: "0:30 Pm", expected: getTodayWithTime(12, 30)}
, {target: "1:40 aM", expected: getTodayWithTime(1, 40)}
, {target: " 6:15 am ", expected: getTodayWithTime(6, 15)}
, {target: " 6:15am ", expected: getTodayWithTime(6, 15)}
, {target: "006:15am", expected: null}
, {target: "06:015am ", expected: null}
, {target: "06:15amm ", expected: null}
, {target: "a6:15am ", expected: null}
, {target: "6: 15am", expected: null}
, {target: "6:15", expected: null}
];
for (var ii = 0; ii < tests.length; ii++) {
var test = tests[ii];
var actual = parseTime(test.target);
console.assert(
(actual === null && actual === test.expected)
|| (actual && test.expected && actual.getTime() === test.expected.getTime())
, ii + 1 + " Failed"
, test.target, test.expected, actual
);
}
console.log('done');
}
parseTimeTests()
function isOneHourGreaterTests() {
var mockTime = getTodayWithTime(14, 00);
var tests = [
{target: "3:01 pm", expected: true}
, {target: "3:30 pm", expected: true}
, {target: "11:59 pm", expected: true}
, {target: "10:30 pm", expected: true}
, {target: "2:59 pm", expected: false}
, {target: "2:01 pm", expected: false}
, {target: "2:30 pm", expected: false}
, {target: "2:00 pm", expected: false}
, {target: "10:30 am", expected: false}
, {target: "1:40 pm", expected: false}
, {target: "1:15 pm", expected: false}
, {target: "6:15 am", expected: false}
, {target: "a6:15 am", expected: null}
];
for (var ii = 0; ii < tests.length; ii++) {
var test = tests[ii];
var actual = isOneHourGreater(test.target, mockTime);
console.assert(
(actual === null && actual === test.expected)
|| (actual === test.expected)
, ii + 1 + " Failed"
, test.target, test.expected, actual
);
}
mockTime = getTodayWithTime(23, 00);
tests = [
{target: "1:01 am", expected: false}
, {target: "12:01 am", expected: false}
, {target: "2:30 am", expected: false}
, {target: "11:59 pm", expected: false}
, {target: "11:00 pm", expected: false}
, {target: "11:01 pm", expected: false}
, {target: "2:30 pm", expected: false}
, {target: "2:00 pm", expected: false}
, {target: "10:30 am", expected: false}
, {target: "1:40 pm", expected: false}
, {target: "1:15 pm", expected: false}
, {target: "6:15 am", expected: false}
, {target: "a6:15 am", expected: null}
];
for (var ii = 0; ii < tests.length; ii++) {
var test = tests[ii];
var actual = isOneHourGreater(test.target, mockTime);
console.assert(
(actual === null && actual === test.expected)
|| (actual === test.expected)
, ii + 1 + " Failed"
, test.target, test.expected, actual
);
}
console.log('done');
}
isOneHourGreaterTests();
brianpeiris
2010-09-27 07:58:13