我已编写代码来计算两次之间的时间,这是我的代码
<html>
<head>
<script>
// java描述
calculateDiff();
function calculateDiff(){
var _start = document.getElementsByName("usr").value;
var _end = document.getElementsByName("usr2").value;
_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);
if (_end_time < _start_time){
_end_time = parseAMDate(_end,1);
}
var difference= _end_time - _start_time;
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0 ){
if (minutes == 0){
minutes = "00";
}
alert(hours+":"+minutes);
}
}
function parseAMDate(input, next_day) {
var dateReg = /(\d{1,2}):(\d{2})\s*(AM|PM)/;
var hour, minute, result = dateReg.exec(input);
if (result) {
hour = +result[1];
minute = +result[2];
if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
}else{
return new Date(1970, 01, 02, hour, minute).getTime();
}
}
//形式
</script>
</head>
<body>
<form>
Select start time:<input type="time" name="usr" id="startTime">
Select end time: <input type="time" name="usr2" id="endTime">
<input type="button" value="cal" onclick="calculateDiff()">
</form>
</body>
</html>
如果我正在执行此操作,我无法获得输出,请帮助我
当我运行此代码时,我正在退出
calculateDiff();
function calculateDiff(){
_start = "01:00 AM";
_end = "02:00 AM";
_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);
if (_end_time < _start_time){
_end_time = parseAMDate(_end,1);
}
var difference= _end_time - _start_time;
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0 ){
if (minutes == 0){
minutes = "00";
}
alert(hours+":"+minutes);
}
}
function parseAMDate(input, next_day) {
var dateReg = /(\d{1,2}):(\d{2})\s*(AM|PM)/;
var hour, minute, result = dateReg.exec(input);
if (result) {
hour = +result[1];
minute = +result[2];
if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
}else{
return new Date(1970, 01, 02, hour, minute).getTime();
}
}
如何获取上述代码?
答案 0 :(得分:0)
问题是_start
和_end
是undefined
:
var _start = document.getElementsByName("usr").value;
var _end = document.getElementsByName("usr2").value;
在你使用之前:
_start = "01:00 AM";
_end = "02:00 AM";
所以它有效。
document.getElementsByName()
正在返回一个数组:
document.getElementsByName("usr")[0].value
<html>
<head>
<script>
//javascript
window.onload = calculateDiff;
function calculateDiff() {
var _start = document.getElementsByName("usr")[0].value;
var _end = document.getElementsByName("usr2")[0].value;
_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);
if (_end_time < _start_time) {
_end_time = parseAMDate(_end, 1);
}
var difference = _end_time - _start_time;
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0) {
if (minutes == 0) {
minutes = "00";
}
alert(hours + ":" + minutes);
}
}
function parseAMDate(input, next_day) {
var dateReg = /(\d{1,2}):(\d{2})\s*(AM|PM)/;
var hour, minute, result = dateReg.exec(input);
if (result) {
hour = +result[1];
minute = +result[2];
if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
} else {
return new Date(1970, 01, 02, hour, minute).getTime();
}
}
//form
</script>
</head>
<body>
<form>
Select start time:
<input type="time" name="usr" id="startTime">Select end time:
<input type="time" name="usr2" id="endTime">
<input type="button" value="cal" onclick="calculateDiff()">
</form>
</body>
</html>
&#13;