Reputation: 159
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?
Upvotes: 1
Views: 4083
Reputation: 693
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);
});
Upvotes: 0
Reputation: 318222
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
Upvotes: 4