Solved

TEXT Month and Year to DATE Month and Year

Posted on 2010-11-10
8
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

728 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