?
Solved

Display Leading Zero in Javascript getDate function

Posted on 2010-01-08
7
Medium Priority
?
714 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:laubacht
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Rakesh Jaimini
ID: 26213778
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
 
LVL 1

Author Comment

by:laubacht
ID: 26213875
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
 
LVL 10

Expert Comment

by:Rakesh Jaimini
ID: 26213927
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:Rakesh Jaimini
ID: 26213944
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 26213973
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
 
LVL 1

Author Closing Comment

by:laubacht
ID: 31674758
Worked perfectly, thank you!
0
 
LVL 1

Author Comment

by:laubacht
ID: 26214024
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month15 days, 9 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question