473,498 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1430
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
1943
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
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
4
3322
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
3898
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
5644
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
3786
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
3361
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
1557
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
4501
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.