Solved

TEXT Month and Year to DATE Month and Year

Posted on 2010-11-10
8
447 Views
Last Modified: 2012-08-14
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
Comment
Question by:s_hausen
  • 3
  • 3
  • 2
8 Comments
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34104125
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34104160
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
 

Author Comment

by:s_hausen
ID: 34104254
we can use any day there but actually i need to compare the month & year to complete date format month & year.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:s_hausen
ID: 34104264
so in case month & year matches the date format month & year, i need to throw an alert for it.
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34104394
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
 

Author Comment

by:s_hausen
ID: 34104573
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
 
LVL 16

Expert Comment

by:jmatix
ID: 34104626
Yes, if the field contains date in "MM/DD/YYYY" format.
0
 
LVL 10

Expert Comment

by:Tyler Laczko
ID: 34104783
I should be awarded points for this as jmatix used my solution
0

Featured Post

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.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

790 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