如何比较日期 [英] How to Compare dates

查看:80
本文介绍了如何比较日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生您好,

我编写了这个脚本,如果输入的开始时间是9:30,结束时间是10:30,那么没人会在另一个文本框中输入9:45

Hello sir,

I made this script that if one enter Starttime is 9:30 and end time is 10:30 then nobody would able to enter 9:45 in another textbox

<script type="text/javascript" language="javascript">
    function CompareTime() {
        var Time = new Array();
        Time[1] = "StartTime";
        Time[2] = "EndTime";
        Time[4] = "EnterTime";
        Time[3] = Time[2] - Time[1];
        if (Time[1] < Time[4] && Time[4] < Time[2] - Invalid) {
            document.write("This is Correct Time");
        }
        else {
            document.write("This is Wrong Time");
        }
    }
</script>


请提示我在做什么错


无效的是关键字


Please hint what I m doing wrong


Invalid is Keyword

推荐答案

仅使用运算符"==",<",>",< =",> =" (它们的含义是自然")和-".最后一个需要说明:它返回结构System.TimeSpan的值,时间点之间的持续时间可以为零,负或正.此类型可以返回分数形式的持续时间值,也可以以许多不同的时间单位返回整个单位的持续时间值,请参阅 http://msdn.microsoft.com/en-us/library/system .datetime.aspx [^ ].

—SA
Just use the operators "==", "<", ">", "<=", ">=" (the meaning of them is "natural") and "-". The last one needs explanation: it returns the value of the structure System.TimeSpan, a duration between time points which can be zero, negative or positive. This type can return duration values in fractional measure and in whole units, in a lot of different time units, please see http://msdn.microsoft.com/en-us/library/system.timespan.aspx[^].

For more detail, see http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

—SA


不要像普通字符串那样比较时间,否则可能会产生错误的结果.这是示例

Do not compare time like common string, it could produce wrong result. here is example

var start = "01:00 PM";
var end = "11:00 AM";
var dtStart = new Date("1/1/2007 " + start);
var dtEnd = new Date("1/1/2007 " + end);
var difference_in_milliseconds = dtEnd - dtStart;
if (difference_in_milliseconds < 0)
{
alert("End date is before start date!");
}


这篇关于如何比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆