time in if/then statement

L

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
 
T

Thomas 'PointedEars' Lahn

ll said:
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
 
J

jamie.ly

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))
{
// ...

}
 
L

ll

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
 
T

Thomas 'PointedEars' Lahn

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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Fri, 3 Aug 2007 18:49:59, (e-mail address removed) 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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] (e-mail address removed) 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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Sat,
4 Aug 2007 11:30:34 said:
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.
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] 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.

Obviously you have still not learned to get rid of your provincial attitude.
When will you ever learn?
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.

I have not recommended anything. I said simply that "12/12/2007" is a
format that works in IE 7 and Firefox 2.0.0.6, as opposed to "1:00 pm";
where in contrast "1:00 pm" works in Opera 9.22. Which is reason enough not
to rely on a specific format.
Evidently you have not read ISO/IEC 16262 carefully enough.

Evidently you are very seldom able to post anything constructive.
Your signature separator is misplaced and your signature is both too
long and malformed.

It's not, and what you are quoting has no relevance to your reply.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top