Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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>

SAMPLE YEAR: 
<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>
          </cfselect>

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

</cfform>

Open in new window

0
s_hausen
Asked:
s_hausen
  • 3
  • 3
  • 2
1 Solution
 
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)
0
 
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
}
0
 
s_hausenAuthor Commented:
we can use any day there but actually i need to compare the month & year to complete date format month & year.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
s_hausenAuthor Commented:
so in case month & year matches the date format month & year, i need to throw an alert for it.
0
 
Justin MathewsCommented:
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
}
0
 
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);
???
0
 
Justin MathewsCommented:
Yes, if the field contains date in "MM/DD/YYYY" format.
0
 
Tyler LaczkoCommented:
I should be awarded points for this as jmatix used my solution
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now