# days between two dates

**URL:** <https://forum.servoy.com/t/days-between-two-dates/881>\
**Category:** Classic Servoy\
**Created:** [December 18, 2003, 5:18pm UTC](https://forum.servoy.com/t/days-between-two-dates/881 "2003-12-18T17:18:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Ron](https://forum.servoy.com/u/Ron)\
**Post date:** [December 18, 2003, 5:18pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/1 "2003-12-18T17:18:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [December 18, 2003, 7:57pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/2 "2003-12-18T19:57:00Z")

</div>

Here’s one way of doing it:

var x = date1 - date2 //substracting two dates returns difference in milliseconds  
var one\_day=1000_60_60\*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

---

<div class="post-metadata">

**Author:** ![Karel\_Broer](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@Karel\_Broer](https://forum.servoy.com/u/Karel_Broer)\
**Post date:** [April 14, 2005, 11:04am UTC](https://forum.servoy.com/t/days-between-two-dates/881/3 "2005-04-14T11:04:45Z")

</div>

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

```auto
var day = new Date() + 30

```

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [April 14, 2005, 12:21pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/4 "2005-04-14T12:21:16Z")

</div>

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)

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

```

globalscript “setDate”

```auto
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.

---

<div class="post-metadata">

**Author:** ![Karel\_Broer](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@Karel\_Broer](https://forum.servoy.com/u/Karel_Broer)\
**Post date:** [April 14, 2005, 6:46pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/5 "2005-04-14T18:46:03Z")

</div>

> 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]( "Very Happy")  
It’s not as easy calculating as with Filemaker, but this method is really nice!

---

<div class="post-metadata">

**Author:** ![sbutler](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/sbutler/32/4393_2.png) [@sbutler](https://forum.servoy.com/u/sbutler)\
**Post date:** [July 4, 2007, 5:25pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/6 "2007-07-04T17:25:43Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Karel\_Broer](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@Karel\_Broer](https://forum.servoy.com/u/Karel_Broer)\
**Post date:** [July 5, 2007, 7:11am UTC](https://forum.servoy.com/t/days-between-two-dates/881/7 "2007-07-05T07:11:25Z")

</div>

![:idea:]( "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](http://www.servoy-plugins.de/dateutils.html)

---

<div class="post-metadata">

**Author:** ![jw\_teunisse](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jw\_teunisse](https://forum.servoy.com/u/jw_teunisse)\
**Post date:** [July 5, 2007, 7:35pm UTC](https://forum.servoy.com/t/days-between-two-dates/881/8 "2007-07-05T19:35:44Z")

</div>

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](http://en.wikipedia.org/wiki/Julian_day) and

[http://www.hermetic.ch/cal\_stud/jdn.htm](http://www.hermetic.ch/cal_stud/jdn.htm)

I have used this algorithms in workflow calculations

best regards  
Jan willem
