473,246 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

comparing times in javascript???

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");
}
Sep 5 '08 #1
3 1424
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.
Sep 5 '08 #2
In comp.lang.javascript message <u113c4lcklncsr1fc72nal0rmgnq89iab3@4ax.
com>, Fri, 5 Sep 2008 19:11:44, Dr***********@nyc.rr.com posted:
>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.
Sep 6 '08 #3
>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.
>Perhaps the OP did not try a search for javascript "time comparison" .
You get first few hits. ;)
>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.
>Using AM & PM in data processing is sheer folly.
Definitely

Sep 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dimension7 | last post by:
All, I am comparing to functions to see which is "better". In better, I mean more efficient, optimize, faster, etc. I have read other posts from other boards, but I'm not really sure of the...
11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
4
by: M.i.r.a.g.e. | last post by:
I am sorry if this has been asked before, but I looked all over for exactly what I need, before asking you guys. I know that it has something to do with the Date object, but I'm not sure how to...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
5
by: JL | last post by:
I need to compare two times. The problem I have is this: In my code I create a time variable using the format statement below: dim firstTime as DateTime fistTime = Format("12:00:00 AM", "T") ...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
8
by: shankwheat | last post by:
I have two strings that I need to compare and modify when there is a matching value in the two. If strA = '12937,' then I need to remove the value '12937,' from strB and the new value would be...
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.