本文介绍了Javascript的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个文本框,我把时间作为HH:MM AM / PM(如下午5:30)。我想验证时间:是否比当前时间大1小时。如何使用JavaScript执行此操作?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?推荐答案首先,我建议使用现有的日期处理库,如 Datejs 。如果经过时间考验的图书馆有一套全面的方法,您将获益。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.使用Datejs,您的答案很简单:With Datejs, your answer is as simple as:Date.parse(timeText).isAfter((new Date()).addHours(1));如果您不想使用Datejs,可以使用正则表达式在验证之前解析时间。我编写了一组函数,以及在Firebug控制台中运行的单元测试,它就是这样做的: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(); 这篇关于Javascript的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 08:04