CSS & Web Design Forum  

Welcome to the CSS & Web Design Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
WM Media Banner

Supporters
Pos Software - POS hardware and Software to track, control and manage your inventory.
Web Templates - BoxedArt is the most reliable source for unlimited template downloads.

Social Bookmarking
WL Marketing - Your #1 source for building links

Go Back   CSS & Web Design Forum > Web Design > AJAX, Javascript & DOM
Reply
 
Thread Tools Search this Thread Display Modes
(#1)
Old
Brian
Guest
 
Default comparing times in javascript??? - 09-05-2008, 07:10 PM

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");
}
Reply With Quote
(#2)
Old
Dr_KralNOSPAM@nyc.rr.com
Guest
 
Default Re: comparing times in javascript??? - 09-05-2008, 07:49 PM

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.
Reply With Quote
(#3)
Old
Dr J R Stockton
Guest
 
Default Re: comparing times in javascript??? - 09-06-2008, 05:33 PM

In comp.lang.javascript message <u113c4lcklncsr1fc72nal0rmgnq89iab3@4ax.
com>, Fri, 5 Sep 2008 19:11:44, Dr_KralNOSPAM@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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Reply With Quote
(#4)
Old
Dr_KralNOSPAM@nyc.rr.com
Guest
 
Default Re: comparing times in javascript??? - 09-06-2008, 10:52 PM


>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

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Hosting at Triple.com
vBulletin Skin developed by: vBStyles.com

Printing Company  Link Building Services