469,287 Members | 2,709 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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

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
2 13314
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
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.

Similar topics

7 posts views Thread by Eddy Soeparmin | last post: by
22 posts views Thread by KitKat | last post: by
6 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.