Ajax get Date in dd / mm / yyyy format

var d = new Date();
    var today_date = d.getDate() + '/' + month_name[d.getMonth()] + '/' + d.getFullYear();

This is how I get the date. It works with a little problem. Today, on June 7, 2011, he is returning on 11/11/2011, what do I want him to return on 07/11/2011?

Does anyone know how?

+3
source share
4 answers

Same:

("0"+1).slice(-2);  // returns 01
("0"+10).slice(-2); // returns 10

Full example:

var d = new Date(2011,1,1); // 1-Feb-2011
var today_date =
    ("0" + d.getDate()).slice(-2) + "/" +
    ("0" + (d.getMonth() + 1)).slice(-2) + "/" + 
    d.getFullYear();
// 01/02/2011
+1
source

Well, you could just check the length d.getDate(), and if it is 1, then you add zero at the beginning. But would you like to take a look at format()to format dates?

+2
source

Try it ( http://blog.stevenlevithan.com/archives/date-time-format ):

var d = new Date();
d.format("dd/mm/yyyy"); 
+1
source

Try this, it is more clear .:

  var currentTime = new Date();
  var day = currentTime.getDate();
  var month = currentTime.getMonth() + 1;
  var year = currentTime.getFullYear();

  if (day < 10){
  day = "0" + day;
  }

  if (month < 10){
  month = "0" + month;
  }

  var today_date = day + "/" + month + "/" + year;
  document.write(today_date.toString());

And the result:

07/05/2011

+1
source

All Articles