TEXT Month and Year to DATE Month and Year

I've two drop downs one for month and one for year and a date textbox. I want to compare the month & year to date month & year using javascript. any help, comments and feedback  would be deeply appreciated.

<cfform name="frm1" action="page2.cfm" method="post">
VISIT DATE: <cfinput name="VisitDate" id="VisitDate" type="datefield" size="15" tabindex="1" validate="date" maxlength="10" required="yes" message="Please enter the date format(mm/dd/yyyy)." /><br>

SAMPLE MONTH: <cfselect name="SampleMonth">

  <option value="01" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "January"> selected</cfif>>January</option>
  <option value="02" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "February"> selected</cfif>>February</option>
  <option value="03" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "March"> selected</cfif>>March</option>
  <option value="04" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "April"> selected</cfif>>April</option>
  <option value="05" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "May"> selected</cfif>>May</option>
  <option value="06" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "June"> selected</cfif>>June</option>
  <option value="07" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "July"> selected</cfif>>July</option>
  <option value="08" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "August"> selected</cfif>>August</option>
  <option value="09" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "September"> selected</cfif>>September</option>
  <option value="10" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "October"> selected</cfif>>October</option>
  <option value="11" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "November"> selected</cfif>>November</option>
  <option value="12" <cfif #MonthAsString(Month(VARIABLES.yourDate))# EQ "December"> selected</cfif>>December</option>

<cfselect name="SampleYear">
          <option value="2020" <cfif #Year(VARIABLES.yourDate)# EQ "2020"> selected</cfif>>2020</option>
          <option value="2019" <cfif #Year(VARIABLES.yourDate)# EQ "2019"> selected</cfif>>2019</option>
          <option value="2018" <cfif #Year(VARIABLES.yourDate)# EQ "2018"> selected</cfif>>2018</option>
          <option value="2017" <cfif #Year(VARIABLES.yourDate)# EQ "2017"> selected</cfif>>2017</option>
          <option value="2016" <cfif #Year(VARIABLES.yourDate)# EQ "2016"> selected</cfif>>2016</option>
          <option value="2015" <cfif #Year(VARIABLES.yourDate)# EQ "2015"> selected</cfif>>2015</option>
          <option value="2014" <cfif #Year(VARIABLES.yourDate)# EQ "2014"> selected</cfif>>2014</option>
          <option value="2013" <cfif #Year(VARIABLES.yourDate)# EQ "2013"> selected</cfif>>2013</option>
          <option value="2012" <cfif #Year(VARIABLES.yourDate)# EQ "2012"> selected</cfif>>2012</option>
          <option value="2011" <cfif #Year(VARIABLES.yourDate)# EQ "2011"> selected</cfif>>2011</option>
          <option value="2010" <cfif #Year(VARIABLES.yourDate)# EQ "2010"> selected</cfif>>2010</option>

<input type="submit" name="submit" value="submit" >


Open in new window

Who is Participating?
Justin MathewsConnect With a Mentor Commented:
var yr = document.forms[0].SampleYear.value;
var mon = document.forms[0].SampleMonth.value;

If your other date variable is dt2 you can compare as:

if (dt2.getMonth()+1==mon && dt2.getFullYear()==yr)
//do something
Tyler LaczkoCommented:
in js you will use:

document.formname.selectname.selected.text to get the text of the selected item.

create a date for the current date using the js date function
create a date for the selected date using the js date function

compare the two and convert back to time.

(Google javascript date function)
Justin MathewsCommented:
Since the day part is missing, can it be assumed to be 01 for all months. If so, you can convert the selection to date as:

var yr = document.forms[0].SampleYear.value;
var mon = document.forms[0].SampleMonth.value;

var dt = new Date(mon+"/01/"+yr);

Then if your other date variable is dt2 you can compare as:

if (dt.getTime() > dt2.getTime())
//do something
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

s_hausenAuthor Commented:
we can use any day there but actually i need to compare the month & year to complete date format month & year.
s_hausenAuthor Commented:
so in case month & year matches the date format month & year, i need to throw an alert for it.
s_hausenAuthor Commented:
Hi jmatix,
I am a newbie in javascript, would u plz tell me where this dt2 value is coming or how to define it in javascript. is this the right way to define it: as according to my code as above.

var dt2=new Date(document.forms.VisitDate.value);
Justin MathewsCommented:
Yes, if the field contains date in "MM/DD/YYYY" format.
Tyler LaczkoCommented:
I should be awarded points for this as jmatix used my solution
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.