Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

HTML
<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
Posted
Updated 10-Nov-11 19:53pm
v2

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
 
Share this answer
 
v2
Do not compare time like common string, it could produce wrong result. here is example

JavaScript
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!");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900