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?
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?
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.
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?
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
}
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()
.
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.
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
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"));
}