select box population with auto select current date

I have 3 select boxes that are populated with the month, day, and year... Jan Feb Mar, 01,02,03 etc etc

Is there a way to auto select the box closest to today's date?
LVL 3
EduskiAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pinaldaveConnect With a Mentor Commented:
Hi Eduski,

what you can do is this.. .in cold fusion
you go as dateformat(#now()#, "mm") now you get the month...
then after you compare this with your select box month... like
<option value="1">1<cfif mymonth eq "1">selected
<option value="2">2<cfif mymonth eq "2">selected

same goes for year use yy, day use dd
this will work.



Regards,
---Pinal
0
 
thecode101Commented:
What language are you using?
0
 
EduskiAuthor Commented:
coldfusion
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ZylochCommented:
Hi

It can be done both client-side and server-side depending on how you want it and what you have available. I will only show you client-side Javascript right now, but it's of your opinion how you want to proceed.

<script language="javascript">
<!--

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

function selectClosestDate(monthField,dayField,yearField) {
   //Here, I'm assuming the year field is a input field, not a drop down. If it is, it can be corrected quite easily.
   var now = new Date;
   var currDate = now.getDate()+1;
   var currMonth = monthArray[now.getMonth()];
   var currYear = now.getYear();

   for (var i=0;i<monthField.options.length;i++) {
      if (monthField.options[i].value==currMonth) {
          monthField.selectedIndex = i;
          break;
      }
   }

   for (var i=0;i<dayField.options.length;i++) {
       if (parseInt(dayField.options[i].text,10)==currDate) {
          dayField.selectedIndex = i;
       }
   }

   yearField.value = currYear;
}


Have this function in <body onload> and supply the monthField, dayField, and yearField. For your monthfield, each of the options have to have a value of "January" or "February" like this:

<option value="January">Jan</option>

fully written out. Nothing needs to be done for the date field.

Regards,
Zyloch
0
 
EduskiAuthor Commented:
beautiful pinal, thanks
0
 
ZylochCommented:
Ahh, sorry stale window.

Regards
0
 
EduskiAuthor Commented:
thanks for your attempts anyway, i appreciate you taking the time
0
 
pinaldaveCommented:
You are welcome. Glad to help you,
Have a good day!
---Pinal
0
All Courses

From novice to tech pro — start learning today.