getTime() bug

R

Russ Chinoy

Has anyone ever noticed that getTime() returns the same value for both today
(5/31/2005) and tomorrow (6/1/2005)?

You can quickly see it with this page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script>
function DoIt() {
today = new Date(2005,5,31);
tomorrow = new Date(2005,6,1);
alert('today=' + today.getTime() + '\ntomorrow=' + tomorrow.getTime());
}
</script>
</head>

<body>
<a href="#" onClick="DoIt()">Click me</a>
</body>
</html>

This bug is present in Firefox 1.0.4, IE 6.0, and Netwscape 7.1 (the three
browsers I have on my machine). It appears to be year-independent, but some
other month boundaries produce the same problem (such as 3/31 and 4/1).
Anyone have a workaround for this? It's causing problems where I'm trying to
compare dates.

Thanks,
Russ Chinoy
 
U

Ulrik Skovenborg

Russ said:
Has anyone ever noticed that getTime() returns the same value for both today
(5/31/2005) and tomorrow (6/1/2005)?

You can quickly see it with this page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script>
function DoIt() {
today = new Date(2005,5,31);
tomorrow = new Date(2005,6,1);
alert('today=' + today.getTime() + '\ntomorrow=' + tomorrow.getTime());
}
</script>
</head>

You have to remember that the month-parameter starts from 0 (January = 0
, December = 11). That means your script should look like this:

today = new Date(2005,4,31);
tomorrow = new Date(2005,5,1);

Before the date's were actually the same because June 31. are the same
as July the 1st.
 
M

Michael Winter

Has anyone ever noticed that getTime() returns the same value for both today
(5/31/2005) and tomorrow (6/1/2005)?
[snip]

today = new Date(2005,5,31);
tomorrow = new Date(2005,6,1);

Month indicies are zero- not one-based, like arrays. The values you've
chosen just so happen to represent the same date: June has 30 days, so
31 wraps to the 1st of July.

The bug in your code, not the user agent's.

Mike
 
E

Evertjan.

Russ Chinoy wrote on 31 mei 2005 in comp.lang.javascript:
Has anyone ever noticed that getTime() returns the same value for both
today (5/31/2005) and tomorrow (6/1/2005)?

You can quickly see it with this page:

No you cannot. It is no bug, but your mistake.
The mistake is not reading nor heeding the specs:

today = new Date(2005,5,31);

this is non-existing 31 June 2005,
which evaluates it 1 July 2005
tomorrow = new Date(2005,6,1);

this is 1 July 2005

alert('today=' + today.getTime() + '\ntomorrow=' +
tomorrow.getTime());

so they SHOULD be the same.

The month value is ZERO BASED !
 
R

Russ Chinoy

Thanks for refreshing my memory on this. I hadn't used getTime in so long
that I forgot about this!
 
E

Evertjan.

Russ Chinoy wrote on 31 mei 2005 in comp.lang.javascript:
[please do not toppost on usenet]
Thanks for refreshing my memory on this. I hadn't used getTime in so
long that I forgot about this!

But your "bug"/problem is not in getTime().
Try this:

today = new Date(2005,4,31);
tomorrow = new Date(2005,5,1);
alert(today)
alert(tomorrow)
alert(today.getTime())
alert(tomorrow.getTime())
 
M

Matt Kruse

Russ said:
This bug is present in Firefox 1.0.4, IE 6.0, and Netwscape 7.1 (the
three browsers I have on my machine).

Readers of this thread should take note.

If a "bug" exists in multiple competing browsers, and they behave the same,
then you can usually bet the farm that it's Programmer Error and not a bug.
;)
 
G

Grant Wagner

Ulrik Skovenborg said:
You have to remember that the month-parameter starts from 0 (January =
0 , December = 11). That means your script should look like this:

today = new Date(2005,4,31);
tomorrow = new Date(2005,5,1);

Before the date's were actually the same because June 31. are the same
as July the 1st.

And some musings as to why June 31 is the same as July 1 in
J(ava)Script, read this <url:
http://blogs.msdn.com/ericlippert/archive/2003/10/06/53150.aspx />

"That's why JScript's attitude towards error cases is not "die at the
first sign of trouble" but rather "muddle along as best you can".
Assign to an undeclared variable? Declare it dynamically. Forget a
semicolon? Insert it. Try to create a date for the 31st of September?
Move it to October 1st. Divide by zero? Return a NaN . Reference an
element beyond the end of an array? Grow the array. All these things
that would be compile time or runtime errors in other languages are just
handled for you in JScript, for better or for worse."
 
D

Dr John Stockton

JRS: In article <v32ne.10421$zb.3278@trndny01>, dated Tue, 31 May 2005
18:30:51, seen in Russ Chinoy
Has anyone ever noticed that getTime() returns the same value for both today
(5/31/2005) and tomorrow (6/1/2005)?

This is an international newsgroup.

Therefore, it is unwise to give dates in FF format which is only
preferred in one or two countries.

You mean 2005-05-31 and 2005-06-01.

You are, of course, also in error in your claim; while there *may* be
true errors (as distinct from peculiar behaviours) remaining in the
javascript date object, it is most unlikely that there could be one in
three well-known browsers relating to the fundamentals of the Gregorian
Calendar.

Comparing dates by comparing the results of getTime is of itself
reliable - though date objects can be compared without explicit getTime
- but there are pitfalls nearby.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top