2

I am trying to get today's date with some specific format:

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

This is not working. What is the proper way of changing my date's format?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
junaidp
  • 10,801
  • 29
  • 89
  • 137

2 Answers2

3

In JavaScript, the months are zero based. For example:

January = 0

February = 1

...

Simply add one to your month:

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

Here's a working fiddle.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • Adding +1 solved the problem , but i am surprised if my system date is less than november it works fine , but if my system date is november or december it doesnt work.. – junaidp Nov 23 '11 at 13:06
3
var curr_Month = d.getMonth() + 1; //months are zero based
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223