-
1. Re: Calculating Previous Month(s) From Current Date
James A Hiltz CET Jan 20, 2014 11:07 AM (in response to mlstechwriter71)The following is the script I used to perform a simmaler task.
// Get date from field
var v = getField("add your reference date field name here").value;
// Get the field values, as numbers
var d1 = getField("add your field name").value;
if (v && d1) {
// Convert string to date
var d = util.scand('mmmm/dd/yyyy', v);
// add d1 as days
d.setDate(d.getDate() + d1);
// Set this field value to the new date
event.value = util.printd("mm/dd/yyyy", d);
} else {
// Clear this field
event.value = "";
}
-
2. Re: Calculating Previous Month(s) From Current Date
GKaiseril Jan 20, 2014 2:56 PM (in response to James A Hiltz CET)Unfortunately the numbers of days in a month or year are not the same for all months or years, so the get and set date methods might not work. Fortunately there are the getMonth() and setFullYear() methods for getting or setting the month or year for a given date.
For the "On Blur" action for the "DateField" you can use:
function GetField(cName) {
// get a field object with error catching;
var oField = this.getField(cName);
if(oField == null) app.alert("Error accessing field named: " + cName, 0, 0);
return oField;
} // end GetField function;function AddMonths(oDate, nMonths) {
// add nMonths to oDate object;
oDate.setMonth(oDate.getMonth() + nMonths);
return oDate; // return adjusted date object;
} // end AddMonths;function Scand(cFormat, cDate) {
var oDate = util.scand(cDateFormat, event.value);
if(oDate == null) app.alert("Error converting " + oDate.valueAsString + " with format: " + cDateFormat, 0, 0);
return oDate;
} // end Scand functon;var cDateFormat = "mm/dd/yyyy"; // format for date strings;
// event value is the start date string;
// 12 months ago;
var o12MonthsAgo = GetField("12MothsAgoDateField")
var oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -12) // subtract 12 months;
o12MonthsAgo.value = util.printd(cDateFormat,oDate);
// 18 months ago;
var o18MonthsAgo = GetField("18MothsAgoDateField")
oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -18) // subtract 18 months;
o18MonthsAgo.value = util.printd(cDateFormat, oDate);
// 36 months ago;
var o36MonthsAgo = GetField("36MothsAgoDateField")
oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -36); // subtract 36 months;
o36MonthsAgo.value = util.printd(cDateFormat, oDate);
// and so on;
Since getting a field object, converting a date string to a date object and adjusting the date object using the getMonth and setMonth methods are repeated several times I have used functions so the repeated code could be reused. -
3. Re: Calculating Previous Month(s) From Current Date
mlstechwriter71 Jan 21, 2014 6:16 AM (in response to GKaiseril)Wow... Thank you! There is no way I could have figured all of this out!
As I have confessed - I don't do alot of scripting, so please excuse the next bit of questions.
Is it possible to report just the Month/Year for the various "##MonthsAgoDateFields"?
Also I set the the Action for "Mouse Up" instead of "On Blur", since that fits the requirements for this document better - and this is working but it is showing the complete date mm/dd/yyyy.
Thank you again for all the help!
-
4. Re: Calculating Previous Month(s) From Current Date
Mohammad Irfan Jan 23, 2014 1:16 AM (in response to mlstechwriter71)If you really don't want to go in details (or not have time to do that)
Here is the simple solution of your problem.
I have written a small function (a few lines)
Put this in document level javascript
// Document level javascript function -----
function getPastDate(textFieldName, months){
var cDate = util.printd("mm/dd/yyyy",new Date(this.getField(textFieldName).value));
var sDate = util.scand("mm/dd/yyyy", cDate);
if ( sDate== null ){
app.alert("Please enter a valid date of the form \"mm/dd/yyyy\".")
}else {
var pDate = new Date(sDate.getFullYear(),(sDate.getMonth() + months),sDate.getDate());
return util.printd("mm/yyyy",pDate);
}
}
Now suppose we have a textbox name "Text1" having date with format "mm/dd/yyyy"
1st parameter is for textbox name (having the date you want to process)
2nd parameter is number of months (to subtract pass a negative value)
If you want to get result in another textbox "Text2" write the code below in the calculate event of Text2
event.value = getPastDate("Text1",-12);
// This will subtract 12 months from "Text1" value
Hope this will help
-
5. Re: Calculating Previous Month(s) From Current Date
mlstechwriter71 Jan 23, 2014 5:51 AM (in response to Mohammad Irfan)Thank you again - it looks like this is going to work!
Very much appreciated!



