By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,508 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,508 IT Pros & Developers. It's quick & easy.

Comparing DateTime........please help

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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>

Jul 20 '05 #2

P: n/a
JRS: In article <40********@10.10.0.241>, seen in
news:comp.lang.javascript, Brian Genisio <Br**********@yahoo.com> posted
at Wed, 21 Jan 2004 07:47:40 :-
prashanth wrote:
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

one smaller than normalnew Date(yy,mm,dd)
new Date(milliseconds) // milliseconds since January 1, 1970

GMT

The above is not a complete list.

OP: you don't give an area-of-use. The code already given will accept,
I believe, both 17/03/04 and 03/17/04 as St Patrick's Day; but not
17/03/04, which is what is most generally used for that date.
Therefore, the form should not be encouraged. 2004/03/17 is safe.

To validate as being a proper date, see below.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.