Connecting Tech Pros Worldwide Help | Site Map

comparing times in javascript???

  #1  
Old September 5th, 2008, 07:55 PM
Brian
Guest
 
Posts: n/a
I need to be able to only allow my clients to enter data into a part
of a form between specific hours of the day. i can find tons of
articles on comparing dates, but not times. does anyone know how to
do this?

this is what i came up with, but its obviously not right. thanks in
advance

var a_p = "";
var d = new Date();

var curr_hour = d.getHours();

if (curr_hour < 12)
{
a_p = "AM";
}
else
{
a_p = "PM";
}
if (curr_hour == 0)
{
curr_hour = 12;
}
if (curr_hour 12)
{
curr_hour = curr_hour - 12;
}

var curr_min = d.getMinutes();

curr_min = curr_min + "";

if (curr_min.length == 1)
{
curr_min = "0" + curr_min;
}

var curr_time = curr_hour +":"+curr_min+" "+a_p;
alert(curr_time);




if (curr_time '8:59 AM' ) && (curr_time < '1:01 PM' ) {


alert("ok");
} else {


alert("nope");
}
  #2  
Old September 5th, 2008, 08:15 PM
Dr_KralNOSPAM@nyc.rr.com
Guest
 
Posts: n/a

re: comparing times in javascript???


All that is much too complex. What does the comparison operator know about
AM and PM?

Get the current hour and minute (as you said) and calculate
60*hour+minutes. Compare that to the start and end times in minutes past
previous midnight (=0).

Be sure to account for local/GMT time with the time function.
  #3  
Old September 6th, 2008, 06:25 PM
Dr J R Stockton
Guest
 
Posts: n/a

re: comparing times in javascript???


In comp.lang.javascript message <u113c4lcklncsr1fc72nal0rmgnq89iab3@4ax.
com>, Fri, 5 Sep 2008 19:11:44, Dr_KralNOSPAM@nyc.rr.com posted:
Quote:
>Get the current hour and minute (as you said) and calculate
>60*hour+minutes. Compare that to the start and end times in minutes past
>previous midnight (=0).

D = new Date()
X = 100*D.getHours() + D.getMinutes()
OK = X 0900 && X < 1300

There is no need for the limitation to 60 minutes in every hour! This
form allows times to be entered as normal un-separated 24-hour clock
figures. It assumes that the OP's "between" means "between"; possibly
one or other bound should really be inclusive.

Perhaps the OP did not try a search for javascript "time comparison" .

On the Web, users are not necessarily local, and it may be better to
work in UTC.

Using AM & PM in data processing is sheer folly.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
  #4  
Old September 6th, 2008, 11:55 PM
Dr_KralNOSPAM@nyc.rr.com
Guest
 
Posts: n/a

re: comparing times in javascript???


Quote:
>There is no need for the limitation to 60 minutes in every hour! This
>form allows times to be entered as normal un-separated 24-hour clock
>figures.
That is nice.
Quote:
>Perhaps the OP did not try a search for javascript "time comparison" .
You get first few hits. ;)
Quote:
>On the Web, users are not necessarily local, and it may be better to
>work in UTC.
That up to the OP -- the requirement might be local or otherewise.
Quote:
>Using AM & PM in data processing is sheer folly.
Definitely

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
jQuery Query about comparing jQuery references Aaron Gray answers 20 July 27th, 2008 02:55 PM
Queues and Javascript engines Safalra answers 1 December 23rd, 2006 05:15 PM
What is an equivalent TimeSerial() function of VBscript in the Java Script?? divya answers 7 September 25th, 2006 06:05 AM
Comparing two 12hr time fields M.i.r.a.g.e. answers 4 July 23rd, 2005 02:22 PM