days between two dates

I guess I am not the only one to find the date calcuations in Servoy more complex than in FMP.
May be someone can give some examples in the HOW TO thread.
For now I have would like to know how to calculate the number of days between two dates.
Thanks.

Here’s one way of doing it:

var x = date1 - date2 //substracting two dates returns difference in milliseconds
var one_day=10006060*24 //ms * sec * min * hrs in a day

var diffExact = x / one_day //gets difference in days
var diffRounded = Math.ceil(diffExact ) // rounds 2.343 to 3

So how can I add a number of days to a certain date? Like for example:

var day = new Date() + 30

These are typical things to put in a global script inside a “utils” module.
Write once, use a lifetime.
(since you can easily re use modules in new projects)

the starting script (on a form)

var aCertainDate = new Date (2004,11,1)//can be any date
aCertainDate = globals.setDate(aCertainDate,5, "+") // calls the global script

globalscript “setDate”

var dateObject = arguments[0] //certain Date
var dateObjectMills = dateObject.getTime(); //convert date to millisec
var nrDaysMills = arguments[1] * (1000*60*60*24)  //convert days to millisec
var operator = arguments[2] // - or +

var resultMills = 0;
if(operator == "-")
{
	resultMills = dateObjectMills - nrDaysMills;
}
else
{
	resultMills = dateObjectMills + nrDaysMills;
}

var resultDate =  new Date(); //create dateObject to return
resultDate.setTime(resultMills); //set it with resultMills

return resultDate

So the basic idea with substracting/adding dates/days is to first convert everything to miilisecs, do the calculation, convert calcResult(millsecs) back to a date object.

maarten:
These are typical things to put in a global script inside a “utils” module. Write once, use a lifetime. (since you can easily re use modules in new projects)

That’s right! Great tip, Maarten, it works perfect! :D
It’s not as easy calculating as with Filemaker, but this method is really nice!

Small modification to Maarten’s script to allow for timezones with daylight savings time like EST/EDT

var dateObject = arguments[0] //certain Date
var dateObjectMills = dateObject.getTime(); //convert date to millisec
var nrDaysMills = arguments[1] * (1000*60*60*24)  //convert days to millisec
var operator = arguments[2] // - or +

var resultMills = 0;
if(operator == "-")
{
   resultMills = dateObjectMills - nrDaysMills;
}
else
{
   resultMills = dateObjectMills + nrDaysMills;
}

var resultDate =  new Date(); //create dateObject to return
resultDate.setTime(resultMills); //set it with resultMills

//check for timezone change, like on EST/EDT timezones
var diff = (resultDate.getTimezoneOffset() - dateObject.getTimezoneOffset()) * (1000 * 60);
resultMills += diff;
resultDate.setTime(resultMills);

return resultDate

:idea: May I also point to the FREE DateUtils plugin from Dr. Maison & Partners.
With this plugin it’s possible to do lot’s of stuff with date and time!
More info at Servoy-Plugins.de - DateUtils plugin

The proper way to calculate days between dates is using Julian Day Number calculations/algorithms, like they use in assurance and banking business.

see http://en.wikipedia.org/wiki/Julian_day and

http://www.hermetic.ch/cal_stud/jdn.htm

I have used this algorithms in workflow calculations

best regards
Jan willem