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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15270614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:12:55  来源:igfitidea点击:

Compare two time values (hh:mm am/pm)

javascripthtml

提问by Techy

I have two time value which is selected from drop down box. The time format is hh:mm am/pm. I need to compare the two dates. I have done this code, but it doesn't work for me.

我有两个从下拉框中选择的时间值。时间格式为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>

The javascript method is

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;
}

回答by Marcel Korpel

If you add a 24h time valueattribute to the optionelements, like

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

<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>

you can easily compare them using

您可以使用以下方法轻松比较它们

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;
}

No need for JavaScript Datemethods.

不需要 JavaScriptDate方法。

回答by Harish Anchu

You must separate amand pmfrom the time value by a space. Also in yor code variables dtEndand dtStartare not defined. In the else part just do it like as folows:

您必须用 将ampm与时间值分开space。同样在您的代码变量中dtEnd并且dtStart未定义。在其他部分,请按照以下方式进行操作:

else if(end == start)
{
   alert("Start time and end time cannot be same");
}

回答by Dodge

try strtotime(http://php.net/manual/en/function.strtotime.php)

尝试strtotimehttp://php.net/manual/en/function.strtotime.php

$firstTime="10:00 am";
$secondTime="3:00 am";
$result =strtotime ($firstTime) > strtotime($secondime);

回答by Gokul Subramaniam

In your HTML Code add space between 'hh:mm' and 'am/pm' in select optionvalue

在您的 HTML 代码中,在选择选项值中的'hh:mm' 和 'am/pm' 之间添加空格

 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;
}
<select id="eventstarttime">
  <option value='10:00 am'>10:00am</option>
  <option value='3:00 pm'>3:00pm</option>
 </select>

 <select id="eventstoptime" onblur="return checktime()">
  <option value='10:00 am'>10:00am</option>
  <option value='3:00 pm'>3:00pm</option>
 </select>

回答by Hardik

you can do it by PHP as bellow:

您可以通过 PHP 执行以下操作:

var d1 = "15:30";
var d2 = "15:36";
if (new Date(d1) < new Date(d2)) {alert('newer')}

Look the reference URls :

查看参考 URls :

jQuery/JS - How to compare two date/time stamps?

jQuery/JS - 如何比较两个日期/时间戳?

how to get current time and then compare them using jquery

如何获取当前时间然后使用jquery比较它们

Using PHP you can do it like :

使用 PHP 你可以这样做:

$time1 = strtotime("15:30");
$time2 = strtotime("15:40");

if($time1<$time2)
{
   echo "time 1 is smaller";
}
else if($time1>$time2)
{
   echo "time 2 is smaller";
}
else
{
   echo "both are same";
}

Hope this will help you

希望能帮到你