Javascript的时差

时间:2010-09-27 05:11:36

标签: javascript time

我有一个文本框,我把时间作为HH:MM AM / PM(如下午5:30)。我想验证时间:是否比当前时间大1小时。我怎么能用JavaScript做到这一点?

1 个答案:

答案 0 :(得分:3)

首先,我建议使用现有的日期处理库,例如Datejs。如果经过时间考验的图书馆有一套全面的方法,您将获益。

使用Datejs,您的答案很简单:

Date.parse(timeText).isAfter((new Date()).addHours(1));

如果您不想使用Datejs,可以使用正则表达式在验证之前解析时间。我编写了一组函数,以及在Firebug控制台中运行的单元测试,它就是这样做的:

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();