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
Compare two time values (hh:mm am/pm)
提问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 value
attribute to the option
elements, like
如果value
向option
元素添加 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 Date
methods.
不需要 JavaScriptDate
方法。
回答by Harish Anchu
You must separate am
and pm
from the time value by a space
. Also in yor code variables dtEnd
and dtStart
are not defined.
In the else part just do it like as folows:
您必须用 将am
和pm
与时间值分开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)
尝试strtotime
(http://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?
how to get current time and then compare them using 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
希望能帮到你