Jump to content

Date problem in dropdown list

- - - - -

  • Please log in to reply
5 replies to this topic

#1
graphic360

graphic360

    Newbie

  • Members
  • PipPip
  • 14 posts
Hello

I want to create an Date of birth dropdown item with javascript. I already create Month and year successfully.

for month this code is used
<script>

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

                document.write('<select name="birthday">');

                	document.write('<option>Month</option>');

					for(i=0;i<months.length;i++){

						document.write('<option value=' + months[i] +'>' + months[i] + '</option>');

						}

				document.write('</select>');

				</script>
and for year this code is used:
<script>

			document.write('<select name="year">Year');

            

			var i = 1994;

			var y = "Year";

			document.write('<option value=' + y + '>' + y + '</option>');// To show Year by Default

            while (i < 2050) {

				document.write('<option value=' + i + '>' + i + '</option>');

				i++;

			}

            document.write('</select>');

			</script>
Now i'm satisfied with Month and date. But I having a problem with The Date However, i can use the above code (year) for date (1 to 31) but Some months are not 31. How can i fix it . Please help me

Best regards
ehsan

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
If this isn't an assignment, use jQueryUI jQuery UI - Datepicker Demos & Documentation

#3
graphic360

graphic360

    Newbie

  • Members
  • PipPip
  • 14 posts
thanks lespauled,
actually i wated to create like this image.[ATTACH=CONFIG]4557[/ATTACH]. I've created it with HTML but February-31 is impossible.

#4
A3MIRAL

A3MIRAL

    Newbie

  • Members
  • PipPip
  • 14 posts
Your easiest option is to use the link from lespauled above.
Or, you could throw an alert error message if the selected day doesn't exist in that month.
Or, if you want to do it the correct, harder way, then you could use jQuery for instance to change the available days based on the selected month.
That may be more hassle than its worth though. Why reinvent the wheel?

#5
graphic360

graphic360

    Newbie

  • Members
  • PipPip
  • 14 posts
Thanks to all

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Also note you have a Date object in javascript.
It updates years, months and everything nicely when days increase.

Run this in your browser and you'll see:

<html>
<head>
<script type="text/javascript">
    function init(){
         var date = new Date(2012, 0, 1);
         for(var i=0 ; i<450 ; i++){
            document.write('<div>' + date.getDate() + ' - ' + (date.getMonth()+1) + ' - ' + date.getFullYear() + '</div>');
            date.setDate(date.getDate()+1);
         }         
    }
</script>
</head>


<body onload="init()">            
</body>
</html>
Note month is zero based :thumbdown:

Date object API: JavaScript Date Object




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users