Expand my Community achievements bar.

Date manipulation

Avatar

Former Community Member

Hi all,

I have Three fields, StartDate(date/time field), EndDate(date/time field) and Months(numeric).

What I want is to calculate the number of months and put them on the Months field.

I know this is easy in FormCalc but I want it in Javascript, please.

Thanks

Andile

8 Replies

Avatar

Level 10

Hi Andile,

I would use something like;

var startDate = util.scand("yyyy-mm-dd", StartDate.rawValue);
var endDate = util.scand("yyyy-mm-dd", EndDate.rawValue);

if (startDate !== null && endDate !== null)
{
var months = Math.abs(endDate.getFullYear() - startDate.getFullYear()) * 12;
months += Math.abs(endDate.getMonth() - startDate.getMonth());
Months.rawValue = months;
}

Regards

Bruce

Avatar

Former Community Member

Hi Bruce,

I'm not sure where am I suppose to put this code, but I tried putting it under the months numeric field under the calculate event.

Unfortunately nothing happens.

Avatar

Level 10

I'm not sure if it is a bug or not but I never seem to get any error messages from code in the calculate event.  That is probably the right place but maybe put a temporary button on the form and put the code in there, hopefully then you will get an error message.

Bruce

Avatar

Former Community Member

I tried the button you suggested, I think there's a bug, because I added a message box just after these two lines:

var startDate = util.scand("yyyy-mm-dd", StartDate.rawValue);
var endDate = util.scand("yyyy-mm-dd", EndDate.rawValue);

xfa.host.messageBox("startDate: "

+ startDate ); --- when I click the button this statement doesnt get executed.

Avatar

Level 10

Seems strange that you aren't getting an error message, you might have to change the reference to your fields to suit the structure of your form.  Do you get any error messages from your scripts?

Avatar

Former Community Member

Hello,

I've changed the code a bit, now they want me to calculate the days instead of months, the code works but its when I change select a different month on the calendar field, for example if my start date is 2010/12/01 and end date is 2010/12/31 I gave correct results, but if my end date is 2011/01/01 thats when I start getting incorrect results..Below is is my code:

var

sDate = util.scand("yyyy-mm-dd",startDate.rawValue);

var

eDate = util.scand("yyyy-mm-dd",endDate.rawValue);

if

(sDate == null || eDate == null )

{

xfa.host.messageBox("Pleae enter a valid date.");

exit;

}

var

nYear = sDate.getFullYear();

var

nMonth = sDate.getMonth() + 1;

var

nDay = sDate.getDate();

var

nYear2 = eDate.getFullYear();

var

nMonth2 = eDate.getMonth() + 1; // 0 based

var

nDay2 = eDate.getDate();

var

totalDays =Math.abs(eDate.getFullYear() - sDate.getFullYear());

totalDays

+=Math.abs(eDate.getMonth() - sDate.getMonth());

totalDays

+=Math.abs(eDate.getDate() - sDate.getDate()) ;

xfa.host.messageBox(" Total Number of days: "

+ totalDays);

Avatar

Level 10

Hi Andile

Try;

const millesecondsPerDay = 86400000; // 24×60×60×1000;
var sDate = util.scand("yyyy-mm-dd",startDate.rawValue);
var eDate = util.scand("yyyy-mm-dd",endDate.rawValue);
var totalDays = Math.round((eDate.getTime() - sDate.getTime()) / millesecondsPerDay);
xfa.host.messageBox(" Total Number of days: " + totalDays);

Regards

Bruce

Avatar

Former Community Member

Hi Bruce,

Thanks a lot for your help & patience, your solution works perfectly.

Thanks once again.

Andile