function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shan876shan876 

Calculate Time????

How does one calculate time in Salesforce...
I tried:
Code:
<html>
<head>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript"> 
function initpage()
{

var d = "{!Delivery_Time__c.Actual_Arrival_Time__c}";
var e = "{!Dispatch_Schedule__c.Arrival_Time__c}";

document.getElementById('div_tag').innerHTML = d.getTime() ;
}
</script>
</head>
<body bgcolor="#F3F3EC" onload="initpage()";> 
<font size="2" face="Verdana">
<div id="div_tag">Loading...</div></font>
</body>
</html>
that did not work... does anyone know the syntax
 

DevAngelDevAngel
I believe you need to use a function to convert from the ISO time format to a dateTime js object. There may be one in the ajax toolkit.
London BenLondon Ben

Hi,

There are 'native' functions to deal with date/time values...

Theoretically - you can just use the following function to get the value into a standard JS date object...
Take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp to see the full use of this object...

Code:
myDate = new Date("{!Delivery_Time__c.Actual_Arrival_Time__c}")
 
In practice - If your users are not using the US locale (ie. they are european etc) I've seen that method return unusual results...
I prefer (even though it is a little long winded to use the following:
Code:
var myDate = new Date();
myDate.setFullYear({!Year(Delivery_Time__c.Actual_Arrival_Time__c)},{!Month(Delivery_Time__c.Actual_Arrival_Time__c)-1},{!Day(Delivery_Time__c.Actual_Arrival_Time__c)});
myDate.setHours({!Second(Delivery_Time__c.Actual_Arrival_Time__c)},{!Minute(Delivery_Time__c.Actual_Arrival_Time__c)},{!Second(Delivery_Time__c.Actual_Arrival_Time__c)});

// Note on the 'month' field to use -1 on the end thanks to the way salesforce stores the month value being 1 based rather than 0 based ;)

Then you can manipulate / calculate to your heart's content using JS date functions

Likewise - to make a JS date into a SOQL-compatible date, use the sforce.internal.dateToString() command, as follows:

Code:
  "select id from opportunity where closedate > " +
  sforce.internal.dateToString(myDate)


 

oh - you need to 'include' the standard salesforce ajax script for the above... "/soap/ajax/10.0/connection.js"

Hope this helps ;)

Ben



Message Edited by London Ben on 03-19-2008 05:23 AM
shan876shan876

wow awesome I will definetly try it that way... but this is what I did(see below) but the issue I have is this is an s-control which displays the difference in time on the screen, now management wants it in the object in the custom field difference... I do not know how to do that... I tried but didn't work... any suggestions???

Code:
<html>
<head>
<script type="text/javascript" language="javascript" src="/js/functions.js"></script> 
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript"> 
function two(x) {return ((x>9)—"":"0")+x};

function three(x) {return ((x>99)–"":"0")+((x>9)˜"":"0")+x};

function time() {
var millennium =new Date("{!Delivery_Time__c.Actual_Arrival_Time__c}") ;
var today=new Date("{!Dispatch_Schedule__c.Arrival_Time__c}");
var cvt=millennium.getTime()-today.getTime();

var sec = Math.floor(cvt/1000);
cvt = cvt % 1000;
t = three(cvt);

var min = Math.floor(sec/60);
sec = sec % 60;
t = two(sec) + ":" + t;

var hr = Math.floor(min/60);
min = min % 60;
t = two(min) + ":" + t;

var day = Math.floor(hr/60);
hr = hr % 60;
t = two(hr) + ":" + t;
t = day + ":" + t;

return t;

var account = new sforce.SObject("Delivery_Time__c");

account.Id = result[0].id;
account.Difference__c = time();

var result = sforce.connection.update([account]);


}

document.write("<font face='aria' color='red'>The Difference in Time is " + time()+"</font>");


</script>
</head>
<body bgcolor="#F3F3EC" onload="time()";> 
</body>
</html>


 

EMHDevEMHDev
I have a similar requirement.  I've written an scontrol that is required to update various custom fields when an opportunity is changed.  However I don't want it to run every time it is viewed, so I thought I would check whether the LastModifiedDate was in the past day, or else the control wouldn't run, as the update would already have been performed (assuming that there isn't a more elegant way to do this, since scontrols can't run from an edit environment).

However, when I tried using your code, the Force.com IDE in Eclipse would not update the object, saying that the Year function required a Date and not a Datetime field (I was passing the Opportunity.LastModifiedDate as the merge field).  How do I get around this?  I've tried simply setting a date variable to the merge field -

        var dateMod =  new Date({!Opportunity.LastModifiedDate});
   
but although this doesn't give an error, the date it sets it to is the 1st of January 1970, which is obviously the default date and it is ignoring my parameter.

Any ideas?  It is such a simple problem, all I want to do is test the difference between the current date/time against the LastModifiedDate and if this is less than a day, call the rest of the functions.

Erica


Message Edited by m62Dev on 03-21-2008 08:33 AM
razzazrazzaz
To my knowledge isn't date format in Salesforce done as yyyy-mm-dd .... so wouldn't you have to convert or format the date that way?
Thanks
shan