Answered
Hi all. I'm in the process of creating a form. I need simple Javascript code to calculate # of days in between two dates.
Thank you,
Thomas
Hi all. I'm in the process of creating a form. I need simple Javascript code to calculate # of days in between two dates.
Thank you,
Thomas
console.show(); // force debugging displayconsole.clear(); // starting date informationvar sStartFormat = 'mm/dd/yyyy';var sStartDateName = 'StartDate'; // verify start date informationconsole.println("starting date format: " + sStartFormat);console.println("starting date field name: " + sStartDateName); // ending date informationvar sEndFormat = 'mm/dd/yyyy';var sEndDateName = 'EndDate'; // verify ending date informationconsole.println("ending date format: " + sEndFormat);console.println("ending date field name: " + sEndDateName); // get the field valuesvar sStartDate = this.getField(sStartDateName).value;var sEndDate = this.getField(sEndDateName).value; // verify date valuesconsole.println("starting date value: " + sStartDate);console.println("ending date value: " + sEndDate); // define some date time constantsvar fSec = 1000; //secondvar fMin = 60 * fSec; // minutevar fHr = 60 * fMin; // hourvar fDay = 24 * fHr; // day // process if there is dataif(sStartDate != '' & sEndDate != '') { // get start date number of days from the epoch date for the date and report progressvar oStartDate = util.scand(sStartFormat, sStartDate); // start date objectconsole.println('start date time object: ' + oStartDate);var fStartMillSec = oStartDate.valueOf(); // milliseconds since epoch dateconsole.println('start date in millisonds: ' + fStartMillSec);var fStartDays = fStartMillSec / fDay; // floating number of daysconsole.println('start date in days: ' + fStartDays);var iStartDays = Math.floor(fStartDays); // whole number of daysconsole.println('start date in whole days: ' + iStartDays); // get end date number of days from the epoch datevar oEndDate = util.scand(sEndFormat, sEndDate); // start date objectconsole.println('end date time object: ' + oEndDate);var fEndMillSec = oEndDate.valueOf(); // milliseconds since epoch dateconsole.println('end date in millisonds: ' + fEndMillSec);var fEndDays = fEndMillSec / fDay; // floating number of daysconsole.println('end date in days: ' + fEndDays);var iEndDays = Math.floor(fEndDays); // whole number of daysconsole.println('end date in whole days: ' + iEndDays); // compute difference in daysconsole.println('difference in days: ' + (iEndDays - iStartDays) );event.value = iEndDays - iStartDays;} else {event.value = '';}
// get the current date objectvar oNow = new Date();// display the current date objectconsole.println('current date object: ' + oNow);// show type ofconsole.println('typeof oNow:' + typeof oNow);// format current date object for displayvar sNow = util.printd('mm/dd/yyyy', oNow);// display formatted date stringconsole.println('formatted current date: ' + sNow);// typeof sNowconsole.println('typeof sNow: ' + typeof sNow);console.println('');// Try to format the current date string var sNowString = util.printd('mm/dd/yyyy', sNow);// display resultconsole.println('formatting of date string: ' + sNowString);console.println('');// let us try to process a date stringvar sMyDate = 'Jan 1, 1970';console.println('sMydate: ' + sMyDate);console.println('typeof sMyDate: ' + typeof sMyDate);// convert string to date objectvar oMyDate = util.scand('mmm d, yyyy', sMyDate);// display oMyDateconsole.println('oMyDate: ' + oMyDate);// type of oMyDateconsole.println('typeof oMyDate: ' + typeof oMyDate);// display object in various formatsconsole.println('oMyDate as mm/dd/yyyy: ' + util.printd('mm/dd/yyyy', oMyDate) );console.println('oMyDate as mmmm d, yyyy dddd: ' + util.printd('mm/dd/yyyy dddd', oMyDate));console.println('sMyDate as mmmm d, yyyy dddd: ' + util.printd('mm/dd/yyyy dddd', sMyDate));
Or the post about [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19419]Calculate age in javascript[/url]. You may need to adjust the final divison for the appropriate time interval.
George Kaiser