473,506 Members | 16,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

time in if/then statement

ll
Hi,
It's been a while since I've worked with javascript time comparison,
and I was wondering how I can say "if time < 15:30"?
The semicolon looks out of place to me, and indeed it has caused an
error.
Any help is greatly appreciated.

Thanks
Louis

Aug 3 '07 #1
7 1581
ll wrote:
It's been a while since I've worked with javascript time comparison,
and I was wondering how I can say "if time < 15:30"?
What time?
The semicolon looks out of place to me,
Probably you mean the colon.
and indeed it has caused an error.
Because there is no such thing as a time expression that could have the
syntax you use.

The following is for local time, whenever local (client) time is. If
you need UTC, use getUTC...(), respectively. Note that the client's
clock must be set correctly for that to work, so you should not rely on
it for critical data:

var d = new Date();
if (d.getHours() < 15 || (d.getHours() == 15 && d.getMinutes() < 30))
{
// ...
}
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 3 '07 #2
Try what Thomas said, but if his instructions are somewhat confusing,
use

// if t is a Date object
var d = t;
// otherwise if t is a string like '1:00 pm', this might work
var d = new Date();
d.setTime ( Date.parse ( t ) );
// go on and compare
if (d.getHours() < 15 || (d.getHours() == 15 && d.getMinutes() < 30))
{
// ...

}

Aug 3 '07 #3
ll
On Aug 3, 1:49 pm, jamie...@gmail.com wrote:
Try what Thomas said, but if his instructions are somewhat confusing,
use

// if t is a Date object
var d = t;
// otherwise if t is a string like '1:00 pm', this might work
var d = new Date();
d.setTime ( Date.parse ( t ) );

// go on and compare
if (d.getHours() < 15 || (d.getHours() == 15 && d.getMinutes() < 30))
{
// ...

}

Many thanks, Thomas and Jamie for your help!
both worked seamlessly.

Kind Regards,
Louis

Aug 3 '07 #4
ja******@gmail.com wrote:
Try what Thomas said, but if his instructions are somewhat confusing,
use

// if t is a Date object
var d = t;
// otherwise if t is a string like '1:00 pm', this might work
var d = new Date();
d.setTime ( Date.parse ( t ) );
*If* the above works for you, then

var d = new Date(t);

should suffice. ES3 15.9.3.2 specifies the value t to be parsed "in
exactly the same manner as for the parse method (section 15.9.4.2); let
V be the time value for this date."

However, your approach fails in my Firefox 2.0.0.6 and Internet Explorer
7, where it produces NaN for "1:00 pm"; a value of "12/12/2007", for
example, works there. "1:00 pm" works in Opera 9.22, though (and does
exactly the same as your approach). (All on Windows XP, locale de-CH.)

Since I can't find anything specified about it, it would seem that the
accepted string syntax is implementation-dependent and maybe even
locale-dependent. So this approach would be not an interoperable one
and should be avoided.
Please quote the minimum of what you are replying to, as described in
the newsgroup's FAQ notes.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not the
best source of advice on designing systems that use javascript.
-- Richard Cornford, <f8*******************@news.demon.co.uk>
Aug 4 '07 #5
In comp.lang.javascript message <11**********************@22g2000hsm.goo
glegroups.com>, Fri, 3 Aug 2007 18:49:59, ja******@gmail.com posted:
>// otherwise if t is a string like '1:00 pm', this might work
var d = new Date();
d.setTime ( Date.parse ( t ) );
Date.parse("1:00 pm") gives NaN in IE6.
>// go on and compare
if (d.getHours() < 15 || (d.getHours() == 15 && d.getMinutes() < 30))
The more intelligent and economical programmer might prefer

if (d.getHours()*24 + d.getMinutes() < 15*24+30) ...

but they are a rare breed.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Aug 4 '07 #6
Dr J R Stockton wrote:
[...] ja******@gmail.com posted:
>// go on and compare
if (d.getHours() < 15 || (d.getHours() == 15 && d.getMinutes() < 30))

The more intelligent and economical programmer might prefer

if (d.getHours()*24 + d.getMinutes() < 15*24+30) ...

but they are a rare breed.
The even more intelligent programmer would multiply the hours with 60
(minutes), not 24 (hours) as he would compare against the number of
minutes passed since local midnight.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not the
best source of advice on designing systems that use javascript.
-- Richard Cornford, <f8*******************@news.demon.co.uk>
Aug 4 '07 #7
In comp.lang.javascript message <46**************@PointedEars.de>, Sat,
4 Aug 2007 11:30:34, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted:
>
However, your approach fails in my Firefox 2.0.0.6 and Internet Explorer
7, where it produces NaN for "1:00 pm"; a value of "12/12/2007", for
example, works there. "1:00 pm" works in Opera 9.22, though (and does
exactly the same as your approach). (All on Windows XP, locale de-CH.)
CH? But 50% of the Swiss are gentlemen.

The form "12/12/2007" can only be recommended for about 3% of dates;
does it mean 12 Dec 2007 or Dec 12 2007? It will not otherwise be taken
as Europeans would wish. The form indicated by "2007/12/25" seems safe
as a new Date() argument, in any location.
>Since I can't find anything specified about it, it would seem that the
accepted string syntax is implementation-dependent and maybe even
locale-dependent.
Evidently you have not read ISO/IEC 16262 carefully enough.

So this approach would be not an interoperable one
and should be avoided.

Your signature separator is misplaced and your signature is both too
long and malformed.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Aug 4 '07 #8

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

Similar topics

4
7678
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
8
2776
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
4
5107
by: Polly | last post by:
I had a macro that ran a parameter query and created and opened an Excel file with the system date as part of the file name, but I had to change the file name by hand. So I converted the macro to...
18
3044
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
13
3018
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
12
3182
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
17
6389
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
1
6052
by: Akinyemi | last post by:
I created a Database which I named "Address". I went through the Control Panel and created a DSN to enable me connect to the Database through ODBC. I then created a Form with the same fields as...
3
3864
by: DontB3 | last post by:
Hi, I'm new in this forum, and i hope someone can help. I'm creating an automatic application that transfer a database from Access -> DBF -> Oracle. When My App try to execute Insert SQL...
4
2116
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= | last post by:
I am very sorry to bring a topic up that has been beaten with an ugly stick..... but... If I want to "fix" an byte array for the life time of a program would it be better allocating it on the...
0
7105
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
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7023
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
7479
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
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3188
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...
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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.