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
  • 369,052
  • 73
  • 620
  • 1,021
junaidp
  • 10,351
  • 29
  • 87
  • 135

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
  • 58,309
  • 18
  • 142
  • 160
  • 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
  • 120,002
  • 45
  • 212
  • 219