Display Leading Zero in Javascript getDate function

I have created a form that populates a dropdown menu with the days of the month.  Problem is I cannot get the days to display the leading zero on the days 1-9.

Code is below, so thanks a ton!
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day

Open in new window

LVL 1
laubachtAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
try (Notice I am generating days 1-31, NOT 0-30 like your code)
for (var i=1; i<=31; i++)
	{
		var d = (i<10? '0'+i : i);
		if( today.getDate()!=i)
		{
			dayfield.options[i]=new Option(  d, d);
		}
		else
		{
			dayfield.options[i]=new Option(d,d, true, true);//select today's day
		}
	}

Open in new window

0
 
Rakesh JaiminiCommented:
how about following
for (var i=0; i<31; i++) 
if(i<10)
dayfield.options[i]=new Option('0'+i, i) 
else
dayfield.options[i]=new Option(i, i) 
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day

Open in new window

0
 
laubachtAuthor Commented:
That works great except when it displays today's date (number) in the dropdown, it includes the zero in all the other days except today (i.e. displays '8' instead of '08' while the rest all display '0#')
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Rakesh JaiminiCommented:
try following :)
for (var i=0; i<31; i++)  
if(i<10 || today.getDate()<10) 
{
dayfield.options[i]=new Option('0'+i, i) ;
dayfield.options[today.getDate()]=new Option('0'+today.getDate(), today.getDate(), true, true) ;
}
else 
{
dayfield.options[i]=new Option(i, i) ; 
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true); //select today's day
}

Open in new window

0
 
Rakesh JaiminiCommented:
better
for (var i=0; i<31; i++)   
{
var CurrentDate=today.getDate();
if(i<10 || CurrentDate<10)  
{ 
dayfield.options[i]=new Option('0'+i, i) ; 
dayfield.options[CurrentDate]=new Option('0'+CurrentDate, CurrentDate, true, true) ; 
} 
else  
{ 
dayfield.options[i]=new Option(i, i) ;  
dayfield.options[CurrentDate]=new Option(CurrentDate, CurrentDate, true, true); //select today's day 
}
}

Open in new window

0
 
laubachtAuthor Commented:
Worked perfectly, thank you!
0
 
laubachtAuthor Commented:
To: rakeshjaimini

Thanks for your input.. the main issue I was having with the latter suggestions was it was adding 0s to all the numbers (i.e. '09, 010, 011, etc).

I appreciate the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.