# Calculating time fields

**URL:** <https://forum.servoy.com/t/calculating-time-fields/3905>\
**Category:** Classic Servoy\
**Created:** [March 18, 2005, 9:52pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905 "2005-03-18T21:52:48Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [March 18, 2005, 9:52pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905/1 "2005-03-18T21:52:48Z")

</div>

As I crawl myself from Filemaker to Servoy ![:wink:]( "Wink") , I can’t get this figured out:

For example:  
fromtime = 12:30:00  
untilltime = 13:15:00

In Filemaker I could do this calculation: fromtime - untilltime = 0:45:00, but how does Servoy handle this?

---

<div class="post-metadata">

**Author:** ![bcusick](https://avatars.discourse-cdn.com/v4/letter/b/ed8c4c/32.png) [@bcusick](https://forum.servoy.com/u/bcusick)\
**Post date:** [March 18, 2005, 10:01pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905/2 "2005-03-18T22:01:44Z")

</div>

You have to make it a date - since there is no “time” datatype:

```auto
var startTime = new Date('1/1/2000 12:30')
var endTime = new Date('1/1/2000 13:15')

var elapsed = (endTime - startTime)/(60*1000)

```

60 = number of seconds in a minute  
1000 = number of milliseconds in a second

---

<div class="post-metadata">

**Author:** ![grahamg](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@grahamg](https://forum.servoy.com/u/grahamg)\
**Post date:** [March 18, 2005, 10:09pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905/3 "2005-03-18T22:09:59Z")

</div>

Hi Karel

Keep going - the journey is worth it.

Servoy/SQL databases do work differently to FM - Maarten Berkenbosch on another thread [http://forum.servoy.com/viewtopic.php?t … =calculate](http://forum.servoy.com/viewtopic.php?t=983&highlight=calculate) posted this code which I’ve found very useful:

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

Hope this helps

Graham Greensall

---

<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 5, 2005, 9:14pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905/4 "2005-04-05T21:14:45Z")

</div>

Thanks fellows for your awesome input!  
Also thanks to HJK who send me this code:

```auto
var newie = new Date(1970,0,1,0,0,0,0)
if(starttime && stoptime)
{
var dif = (stoptime - starttime) / (1000 * 60 * 60)
}
else
{
var dif = 0
}
if(customtime)
{
var custom = (customtime - newie) / (1000 * 60 *60) 
}
else 
{
var custom = 0
}
if(breaktime)
{
var breaking = (breaktime - newie) / (1000 * 60 * 60)
}
else
{
var breaking = 0
}
return (dif + custom) - breaking

```

The trick is to have a reference date and time. In this case it is 01-01-1970 0:00:00. From that point we’re able to calculate any time or date!  
Hope this helpes any other person who bumps into this issue ![:wink:]( "Wink")

---

<div class="post-metadata">

**Author:** ![grahamg](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@grahamg](https://forum.servoy.com/u/grahamg)\
**Post date:** [April 5, 2005, 10:40pm UTC](https://forum.servoy.com/t/calculating-time-fields/3905/5 "2005-04-05T22:40:09Z")

</div>

Another way to work with dates is to use following to get yyyymmdd:

> var daterequired = (required\_date.getFullYear() \* 10000) + (required\_date.getMonth() \* 100) + (required\_date.getDate());

If [required\_date] is 6 April 2005 this produces 20050406 which can be compared/calculated etc. Also use a Calculated Field producing yyyymm as basis for producing monthly reports.

Hope this helps

Graham Greensall  
Worxinfo Ltd

---

<div class="post-metadata">

**Author:** ![friedrich\_toussaint](https://avatars.discourse-cdn.com/v4/letter/f/73ab20/32.png) [@friedrich\_toussaint](https://forum.servoy.com/u/friedrich_toussaint)\
**Post date:** [April 8, 2005, 9:35am UTC](https://forum.servoy.com/t/calculating-time-fields/3905/6 "2005-04-08T09:35:50Z")

</div>

high karel,

here is a link to a methode:

[http://forum.servoy.com/viewtopic.php?t=3565](http://forum.servoy.com/viewtopic.php?t=3565)

friedrich toussaint

---

<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 8, 2005, 10:41am UTC](https://forum.servoy.com/t/calculating-time-fields/3905/7 "2005-04-08T10:41:53Z")

</div>

> friedrich toussaint:  
> high karel,  
> here is a link to a methode:  
> [http://forum.servoy.com/viewtopic.php?t=3565](http://forum.servoy.com/viewtopic.php?t=3565)  
> friedrich toussaint

That’s a real neat method, Friedrich! Thanks and keep up the good work!
