473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getTime() bug

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
Jul 23 '05 #1
8 6088
Russ Chinoy wrote:
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.
Jul 23 '05 #2
On 31/05/2005 19:30, Russ Chinoy wrote:
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
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 !

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
Thanks for refreshing my memory on this. I hadn't used getTime in so long
that I forgot about this!

"Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
news:42***********************@news.sunsite.dk...
Russ Chinoy wrote:
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.

Jul 23 '05 #5
Russ Chinoy wrote on 31 mei 2005 in comp.lang.javascript:
"Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
news:42***********************@news.sunsite.dk...
Russ Chinoy wrote:
> 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.


[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())

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #6
Russ Chinoy wrote:
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.
;)

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #7
"Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
news:42***********************@news.sunsite.dk...
Russ Chinoy wrote:
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.


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/ar.../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."

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #8
JRS: In article <v32ne.10421$zb.3278@trndny01>, dated Tue, 31 May 2005
18:30:51, seen in news:comp.lang.javascript, Russ Chinoy
<rc*****@platinumwebworx.com> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
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.
Jul 23 '05 #9

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

Similar topics

2
11633
by: Phil Powell | last post by:
I am forced to have to compare the value of some PHP timestamp with a Java-based java.util.Date.getTime() timestamp. Let me show you how they look: The Java time is returned to the PHP...
2
4138
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
1
2208
by: Shane | last post by:
I have this peice of Javascript, im trying to convert in to VB.net. Everything work ok. I almost get the some value, but the last 5 numbers are all Zeros on my VB out put. the Java version has...
3
3983
by: Matthew Ferri | last post by:
I'm trying to display the server time on the local browser, just as http://www.time.gov does it. Is there any sample code for JS or VB available? TIA,
3
1468
by: Xiangliang Meng | last post by:
Hi, all class Clock { Clock() { Time_m = 0;} void setTime(unsigned int time = 0) { Time_m = time; } unsigned int getTime() const
3
12314
by: Yann Laviolette | last post by:
Hi! I look for a function that work with linux to get the date and the time. Can somebody help me? Thanks Yann
2
7288
by: Shane Saunders | last post by:
I trying to make a sub that works like Javascripts .getTime method. It work ok, but it does not should the time in it. The last numbers should be the time, but they are all zeros. Can anyone help...
12
14671
by: anj2k5 | last post by:
Hello , I want to create a function in C#.net which is equivalent to gettime(milliseconds since midnight 1st January, 1970 ) function in javascript. Actually i need to calculate unique value in...
7
34471
by: NitinSawant | last post by:
Hi, i'm newbie in C#, Consider following function in java: int getMilliseconds(){ Date dt=new Date(); return(dt.getTime()); } pls help me to convert this in C#
0
7136
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
7344
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,...
0
7412
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
7069
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
5652
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
4730
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
3216
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
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...

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.