5 Replies Latest reply on Dec 30, 2013 9:30 PM by AGSanchezYo

# How do I calculate how many days are left in a month?

I'm trying to figure out a way to do this to make a payment apportion using the days left. One of the problems lies in the date being dynamic, since I'm using "whatever today's date is" and it actualizes by day. Any help will be appreciated.

• ###### 2. Re: How do I calculate how many days are left in a month?

Date and time strings are character strings and depending upon the arrangement and structure might not sort into an ascending order and cannot be directly used in a computation. Before any comparison or computation can be performed, one needs to converted to an numerical value and in an ascending order from the earliest date to the latest date. This can easily be done using the util.scand method to convert the string to the date object. This object has a number of properties and methods that can provide the number of milliseconds from the default starting date time of midnight January 1, 1970. One can also get the year, month, date, day of the week, etc from the object with various methods. One could also set or reset the date object to a different date time using the methods of the date object.

So one could convert a date string to the date object and determine the month. Since all but February have a fixed number of days one can easily determine the end of the month. For February one needs to access the full 4 digit year and determine if the year is evenly divisible by 4 and not a century year or the century is divisible by 4 for a leap century year.

Or one needs to set the date to the first of the next month and then adjust the date by -1` day.

• ###### 3. Re: How do I calculate how many days are left in a month?

// I have tried to write a small function

function getRemainingDays(){

var cDate = new Date();

var fDate = new Date(cDate.getFullYear(),(cDate.getMonth() + 1),1);

var mSec = fDate.getTime() - cDate.getTime();

return Math.floor(mSec/86400000);

}

// Hope this will help

• ###### 4. Re: How do I calculate how many days are left in a month?

Using some reusable document level scripts for general functions like getting a field object, a date object from a string, and the number of days in a month one could have a script like:

// document level functions;
function GetField(cName) {
// return the field object for named field;
// with error checking;
var oField = this.getField(cName); // get field object;
// test for error getting field;
if(oField == null){
// error accessing field;
app.alert("Error accessing field " + cName);
}
// return field object;
return oField;
} // end GetField object;

function GetDateObject(cFormat, cDate) {
// get the date object for date cDate with format of cFormat;
var oDate = util.scand(cFormat, cDate);
if(oDate == null) {
app.alert("Error converting date " + cDate + " with format " + cFormat, 0, 0);
}
return oDate;
} // end GetDateObject function;

function DaysInMonth(cFormat, cDate) {
// return last day of month;
var oDate = GetDateObject(cFormat, cDate);
var StartDate = oDate.getDate(); // get day from date object;
// set date object to 1st of next month;
oDate.setMonth(oDate.getMonth() + 1, 1);
// set date object to pervious date;
oDate.setDate(oDate.getDate() - 1);
// get last day of month;
return oDate.getDate();
} // end DaysInMonth function;
// end document level functions;

// custom calculation script;
// check for needed functions;
if(typeof GetField != "function") app.alert("Missing GetField function!", 0, 0);
if(typeof GetDateObject != "function") app.alert("Missing GetDateObject function!", 0, 0);
if(typeof DaysInMonth != "function") app.alert("Missing DaysInMonthh function", 0, 0);

// some specific data to get value from field;
var cDateName = "MyDateField"; // name of date field;
var cDateFormat = "dd-mmm-yyyy"; // format of date field;
// end specific data;
// get date field object;
var oDateField = GetField(cDateName);
// get the string value for the date;
var cDateValue = oDateField.value;
// convert date value to date object;
var oDate = GetDateObject(cDateFormat, cDateValue);
var nStartDate = oDate.getDate(); // start date for computation;
// the day for the last day of the month
var nLastDayOfMonth = DaysInMonth(cDateFormat, cDateValue);
var DaysToEndOfMonth = nLastDayOfMonth - nStartDate;
event.value = DaysToEndOfMonth;
// end custom calculation script;