Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted

# S-Control functions (simple math question)

Hi,

I have a problem:
I have two date/time fields for when a service issue begins until it is resolved:

ServiceIssuesStartDate__c
ServiceIssuesEndDate__c

How do I create a new S-control that calculates only the number of minutes between the begin and end times and have this field in the case page layout?
Everything I do is giving me errors due to incompatible fields date and date/time don't go together.

Signed,
At a loss...
:)
bryano
To calculate the number of minutes between two dates with JavaScript, you can do the following:

1. Create date object for start date
2. Create date object for end date
3. Subtract Start Date from End Date (this gives you time in milliseconds)
4. Divide the milliseconds by the product of 1000 (to get seconds) * 60 (to get minutes)

Message Edited by bryano on 03-28-2007 06:24 PM

Thanks.
I still have a problem that it's not quite calculating the data.
I must be doing something wrong.

Should I be using standard case fields or something more complicated?
I created a formula field (number) which subtracts start time from end time but it still comes up as "0" no matter how I change the formular.

:(
Greg H
Here is a function which should get you what you want:
Code:
```function DateDifference(ServiceIssuesStartDate__c,ServiceIssuesEndDate__c) {
var startDate = new Date(); //create a date variable for Start date
startDate = ServiceIssuesStartDate__c; //assign the date variable to the start date passed to the function
var endDate = new Date(); //create a date variable for End date
endDate = ServiceIssuesEndDate__c; //assign the date variable to the end date passed to the function
var Result = endDate - startDate; //subtract the two dates to determine milliseconds
Result = Result * 1000 * 60; //multiply by 1000 to get seconds then 60 to get minutes
return Result;
}```

Sometimes date values can be tricky when doing calculations so if you still get errors with your dates after trying something like this function then you should create a separate function to break your passed date values down to month, day and year values then pass back the combined values in a single variable - then do your math calcs.
-greg
bryano
I'm a bit confused as to why you're using a formula field to calculate the number of minutes between two dates.  If the ServiceIssueStartDate__c and ServiceIssueEndDate__c are of date/time data type you should be able to create a function that does the steps I posted to calculate the number of minutes.  Looking back at what I wrote, I realized I forgot to tell you that you need to divide the number of milliseconds by the product of (1000 *60).  So, your function would look something like below.

function getNumberOfMinutes(startDate, endDate) {
var start = new Date(startDate);
var end = new Date(endDate);
var minutes = (end - start) / (1000 * 60);
return minutes;
}

Then you  would pass in your service issues dates to the function and what ever value is returned from getNumberOfMinutes(), set that value to the field on the Case and display it.