prashanth wrote:
Hi all,
I am unable to write a javascript which will allow me to compare
datetime entered by the user with the current datetime.( i.e the
usertime should be less than or greater than currenttime)
Thanx a lot in advance
Here is a start... in this example, you can type two forms of the date
in the box:
January 20, 2004
January 20, 2004 10:30:22
Of course, they need to be entered in exactly, so you will likely set up
your form for better user input, and create the string yourself.
The date constructor has the following formats:
new Date("Month dd, yyyy hh:mm:ss")
new Date("Month dd, yyyy")
new Date(yy,mm,dd,hh,mm,ss) // where month is an integer
new Date(yy,mm,dd)
new Date(milliseconds) // milliseconds since January 1, 1970
Here is the code I came up with... play with it how you need:
<HTML>
<HEAD>
<SCRIPT>
function compareDate()
{
var currentDate = new Date();
var userDate =
new Date(document.getElementById("user_date").value);
if(userDate <= currentDate)
alert("SUCCESS");
else
alert("FAILURE");
return false;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT TYPE=text ID=user_date>
<INPUT TYPE=submit onClick="return compareDate();">
</FORM>
</BODY>
</HTML>