views:

833

answers:

7

The following script returns 20 instead of 21!

var d = new Date("2010/03/21");
document.write(d.getDate());

What am I doing wrong? Is this a JavaScript bug?

+1  A: 

I tested this code in Firefox 3.6 and IE 8:

<script type="text/javascript">

var d = new Date("2010/03/21");
document.write(d.getDate());

</script> 

It's showing the right date: 21

For more information, look at: JavaScript Date Object

You can also read about JavaScript Compatibility considerations at Wikipedia.

Leniel Macaferi
it's correct for 2010/03/22 and 2010/03/20 but not for 2010/03/21. look here: http://www.kimag.es/share/84788221.png
Anubis
@Anubis: I can't see the picture you linked.
Leniel Macaferi
+2  A: 

I wrote the following code in my browser's address bar and the result was 21

javascript:alert(new Date("2010/03/21").getDate())

There is no such thing as a Javascript bug, since there is many Javascript implementations, so you'll want to refer to a specific implementation.

The implementation I tested was Chrome 4.1.249. What's yours?

Jader Dias
sorry! it's 20 here!http://www.kimag.es/share/84788221.png
Anubis
i also have tested it on jsbin and it work nice! it return 21! ;-)
aSeptik
I take issue with the statement *'There is no such thing as a JavaScript bug'* https://bugzilla.mozilla.org/buglist.cgi?quicksearch=javascript
John K
@jdk: I think he was referring to the fact there's many implementations of "JavaScript", so what may be a bug in one implementation isn't a bug in "JavaScript" itself, just a bug in the implementation. So really, it's not an incorrect statement to make.
Andy E
+2  A: 

The Date.parse method is implementation dependent (new Date(string) is equivalent to Date.parse(string)).

While this format will be available on modern browsers, you cannot be 100% sure that the browser will interpret exactly your desired format.

I would recommend you to manipulate your string, and use the Date constructor with the year, month and day arguments:

// parse a date in yyyy-mm-dd format
function parseDate(input) {
  var parts = input.match(/(\d+)/g);
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
CMS
+1 - my sentiments exactly
Russ Cam
+2  A: 

Any chance it's treating the string argument as UTC and the resulting Date object as local time, or vice versa? That could throw it off. Compare d.getDate() to d.getUTCDate().

eyelidlessness
beat me to it. I was busy doing a proof before I shot off my mouth. lol.
Sky Sanders
Pays to shoot off your mouth. I just tested that the method existed lol.
eyelidlessness
A: 

http://jsbin.com/aqoki3/edit - here with code you posted I get 21. So its not JS bug. Maybe its bug in your browser implementation (of JS).
So to correctly init your date var use new Date(year, month, date [, hour, minute, second, millisecond ]) (https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date). This way you will be sure that day is 21 becouse you implicitly set it to 21.

NilColor
A: 

You cant't create Date object by passing string argument shows like date.

Create Like this way.

var str_date = "2010/03/21"; var date = new Date();

var date_elements = str_date.split("/");

date.setYear(date_elements[0]); date.setMonth(date_elements[1]); date.setDay(date_elements[2]);

alert(date.toString);

give attention on date.toString method. it returns full date String. date.getDate will only give you day of that day.

for more info of Date object, refer www.w3school.com

Parth
A: 

I get the same date (21) in firfox,safari,chrome,opera and IE, whether I use the string "2010/03/21" or the integer date arguments (2010,2,21).

I am using a windows machine and new versions of the browsers-

you can test to see if your client parses a different date from the string, which is the only place I see for a problem.

if(new Date("2010/03/21")- new Date(2010,2,21)){
alert(new Date("2010/03/21"));
} 
kennebec