Date Manipulation on Form Text Fields

Results 1 to 2 of 2

Thread: Date Manipulation on Form Text Fields

  1. #1
    Join Date
    Dec 1969

    Default Date Manipulation on Form Text Fields

    I have 3 text fields on a form, and the idea is to get the difference between two entered dates before transfer information to database.<BR><BR>FieldOne can get Date from a Popup Calendar or typed by user (FROM Value)<BR>FieldTwo works the same way FieldOne Does, getting a second date value (TO Value).<BR>Field Three has to get the difference. Number of days from One to Two (STAY Value).<BR><BR>By default, FieldOne gets current date from Date(), and second and third fields are empty.<BR><BR>I need to set a suggested date for FieldTwo, when FieldOne change. (FieldOne OnChange = FieldTwo.Value = FieldOne.Value + X) and FieldThree.Value = X.<BR><BR>Also, if user change the suggested date value for FieldTwo, I have to set FieldTwo OnChange = FieldThree.value = (FieldTwo.Value – FieldOne.Value)<BR><BR>Any Idea will be really appreciated.<BR><BR>Tks <BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default Just need to read JS docs...

    There&#039;s no DateAdd or DateDiff in JavaScript, so the only practical way to add date elements is to convert a given date to milliseconds, add the number of elements times the number of milliseconds in one element, and convert the milliseconds back to a date. Similarly, to get the difference you convert both dates to milliseconds, subtract, and convert that number to a count of elements by dividing by the number of milliseconds in one element.<BR><BR>For days, the magic number is 24*60*60*1000 or 86400000.<BR><BR>So:<BR><BR>&#060;SCRIPT&#062;<BR> suggestedDayCount = 7; // or whatever you want it to be<BR><BR>function suggestDate( )<BR>{<BR> var frm = document.FormName;<BR> var dt1 = new Date( frm.FieldOne.value ); // may not work in older browsers!<BR> var ms1 = dt1.getTime(); // milliseconds<BR> var ms2 = ms1 + suggestedDayCount * 86400000;<BR> var dt2 = new Date( ms2 );<BR> frm.FieldTwo.value = dt2.toString(); // may want to format it better<BR>}<BR><BR>function getDifference( )<BR>{<BR> var frm = document.FormName;<BR> var dt1 = new Date( frm.FieldOne.value );<BR> var dt2 = new Date( frm.FieldTwo.value );<BR> var ms1 = dt1.getTime();<BR> var ms2 = dt2.getTime();<BR> var msdiff = ms2 - ms1;<BR> frm.Difference.value = msdiff / 86400000;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>Something along those lines, at least.<BR><BR>Start here:<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts