比较两个时间值(hh:mm am/pm)

Tec*_*chy 0 html javascript

我有两个时间值,从下拉框中选择.时间格式是hh:mm am/pm.我需要比较这两个日期.我已经完成了这段代码,但它对我不起作用.

<select id="eventstarttime">
  <option>10:00am</option>
  ........
  <option>3:00pm</option>
 </select>

 <select id="eventstoptime" onblur="return checktime()">
  <option>10:00am</option>
  ........
  <option>3:00pm</option>
 </select>
Run Code Online (Sandbox Code Playgroud)

javascript方法是

 function checktime()
{
    var start = document.getElementById("eventstarttime").value;
    var end = document.getElementById("eventstoptime").value;


    if(Date.parse('01/01/2011 '+end) < Date.parse('01/01/2011 '+start))
    {
        alert("End time should exceed the start time");
    }
    else if(Date.parse('01/01/2011 '+end) -Date.parse('01/01/2011 '+start)==0)
    {
        alert("Start time and end time cannot be same");
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*pel 7

如果valueoption元素添加24小时属性,例如

<select id="eventstarttime">
  <option value="1000">10:00am</option>
  <option value="1215">12:15pm</option>
  <option value="1500">3:00pm</option>
</select>

<select id="eventstoptime" onblur="return checktime()">
  <option value="1000">10:00am</option>
  <option value="1215">12:15pm</option>
  <option value="1500">3:00pm</option>
</select>
Run Code Online (Sandbox Code Playgroud)

你可以轻松地比较它们

function checktime()
{
    var start = document.getElementById("eventstarttime").value;
    var end = document.getElementById("eventstoptime").value;

    if (end < start)
    {
        alert("End time should exceed the start time");
    }
    else if (end == start)
    {
        alert("Start time and end time cannot be same");
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

不需要JavaScript Date方法.