Solved

TEXT Month and Year to DATE Month and Year

Posted on 2010-11-10
8
439 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now