1

I'm tring to get today's day, month and year from a timestamp with jquery.

 var d = new Date();  //timestamp
 var da = d.getDay();   //day
 var mon = d.getMonth();   //month
 var yr = d.getFullYear();   //year
 var thisDay = da + "/" + mon + "/" + yr;
 alert(thisDay);

it is returning 2/8/2014....please what am i not getting right?

Obi Ik
  • 161
  • 2
  • 9
  • Try `d.getDate()`for the day of the month (2 is because it is Tuesday) `8` is the zero-order integral for the 9th month. – ne1410s Sep 23 '14 at 18:58

2 Answers2

4

getDay gets the day of the week, getDate get's the date of the month.
getMonth is zero based, so you have to add 1.

    var d = new Date();         //timestamp
    var da = d.getDate();       //day
    var mon = d.getMonth() + 1; //month
    var yr = d.getFullYear();   //year
    var thisDay = da + "/" + mon + "/" + yr;
    document.body.innerHTML = thisDay;

If you want 23/09/2014 you have to zero pad the date and month as well, here's how

Community
  • 1
  • 1
adeneo
  • 293,187
  • 26
  • 361
  • 361
0

To get Current day use getDate() and getMonth() always return month 1 less so we need to add +1.

Try Below code

 Jquery:

 $(document).ready(function(){
  var d = new Date();  //timestamp
  var da = d.getDate();//day
  var mon = d.getMonth()+1;   //month
  var yr = d.getFullYear();   //year
  var thisDay = da + "/" + mon + "/" + yr;
  alert(thisDay);
  });
Nandha
  • 625
  • 1
  • 6
  • 14