...

View Full Version : how to return date with leading zero's



needsomehelp
01-05-2012, 02:38 AM
I have the following which returns the date in the format I need it in but it does not have the leading zero's



var wholeDate = getTheDay();

function getTheDay(){

var d = new Date();
var year = d.getFullYear();
var numMonth = d.getMonth() + 1;
var day = d.getDate();
var whole = year + '-' + numMonth + '-' + day;
alert(whole);
return whole;

}


Would return todays date as 2012-1-5

How do I get the date returned like

2012-01-05 ?

qwertyuiop
01-05-2012, 02:57 AM
How about this?



var wholeDate = getTheDay();

function getTheDay(){

var d = new Date();
var year = d.getFullYear();
var numMonth = (d.getMonth() > 8 ? "" : "0") + (d.getMonth() + 1);
var day = (d.getDate() > 9 ? "" : "0") + d.getDate();
var whole = year + '-' + numMonth + '-' + day;
alert(whole);
return whole;

}

Logic Ali
01-05-2012, 03:20 AM
This example uses the declared variable in the expression that determines its own value:

<script type='text/javascript'>

function getTheDay()
{
var d = new Date();
var year = d.getFullYear();
var numMonth = ( numMonth = d.getMonth() + 1 ) < 10 ? "0" + numMonth : numMonth;
var day = ( day = d.getDate() ) < 10 ? "0" + day : day;

return year + '-' + numMonth + '-' + day;
}


alert( getTheDay() );

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum