1

I am trying to change my date format

                var d = new Date();
                var curr_year = d.getFullYear();
                var curr_Month = d.getMonth() +1;
                var curr_date = d.getDate();
                var todayDate =  var tDate =   ( curr_Month +"/"+curr_date +"/"+ curr_year);

This is working perfectly fine ,But for the month November it shows 10,instead of 11 and for the month of December it shows 11 instead of 12 ...for the rest of months it all fine ..

NOTE : This problem is only in mozilla and opera ,in internet explorer its working fine !

Any idea

Thanks

junaidp
  • 10,801
  • 29
  • 89
  • 137

3 Answers3

1

You code should output "11/23/2011" without error.

For the Date element, getMonth() is 0-based. And Jan - Dec is displayed as 0-11.

Reference

getMonth

Returns the month (0-11) in the specified date according to local time.

Community
  • 1
  • 1
steveyang
  • 9,178
  • 8
  • 54
  • 80
1

This works fine for me, producing 11/23/2011 which is the current date in my timezone as of this answer, despite being that weird USA format :-)

This was tested in both Firefox 8 and IE 9 with the following HTML file:

<html><head></head><body><script language="javascript">
var d = new Date();
var curr_year = d.getFullYear();
var curr_Month = d.getMonth() + 1;
var curr_date = d.getDate();
var todayDate =  (curr_Month + "/" + curr_date + "/" + curr_year);
alert (todayDate);
</script></body></html>

That last line of yours is not syntax I've seen before and, in fact, it stops the script from working at all. In IE9, you get:

SCRIPT1002: Syntax error xyz.html, line 6 character 18

and Firefox 8 gives you:

[23:12:17.613] syntax error @ file:///C:/Users/Pax/Documents/xyz.html:6
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

It seems perfectly fine here. What browser do you use (although I don't think it matters)?

lalibi
  • 3,057
  • 3
  • 33
  • 41