Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

When i click on this custom button i need to fill a field with the current date and time.But some times its giving me a previous date i.e exactly 1 day prior to current date..i dont know why this is happpening..can some help me out in this..may be there is some issue with javascript...or timezone issue..as its not happening everytime...but sometime..

var SO = new sforce.SObject("Sales_Order__c");

    function fixTime(time){
       if(time < 10) {time = "0" + time};
       return time;
    }
     function fixDate(date){
      var Month = fixTime(date.getMonth() + 1);
      var Day = fixTime(date.getDate());
      var UTC = date.toUTCString();
      var Time = UTC.substring(UTC.indexOf(':')-2, UTC.indexOf(':')+6);
      var Minutes = fixTime(date.getMinutes());
      var Seconds = fixTime(date.getSeconds());
      return date.getFullYear() + "-" + Month + "-" + Day + "T" + Time;  
    }

    SO.date__c = fixDate(new Date());
updateSO = sforce.connection.update([SO]);
share|improve this question

4 Answers 4

up vote 8 down vote accepted

You're getting the local time zone year, month and day, but UTC time hours and minutes. When it's today here and tomorrow in UTC, your date calculations will become a day behind. Example follows:

Local time: 2013-12-31 22:00 -05:00
UTC time: 2014-01-01 03:00 -00:00

Year = 2013
Month = 12
Day = 31
Time = 03:00

So, you're committing the value of '2013-12-31T03:00Z' instead of '2014-01-01T03:00Z'.

There is no need for any of this code at all. The function sforce.internal.dateTimeToString automatically converts the date/time coordinate to the correct value for you as long as it is a native Date object.

In other words, change your code to:

var SO = new sforce.SObject("Sales_Order__c");
SO.Date__c = new Date();
SO.Id = "{!Sales_Order__c.Id}";
updateSO = sforce.connection.update([SO]);

Do not call sforce.internal.dateTimeToString, as it is already called for you, and as the "internal" suggests, you should not be using that function explicitly.

Edit You need to include the ID value to update a record. This is most likely the problem.

share|improve this answer
    
I am getting an fault string error when using this.can you help –  miku Oct 4 '13 at 6:11
    
You also need to include a record ID. The dates are just fine as described in this answer. –  sfdcfox Oct 4 '13 at 12:07
    
Thanks mate it worked..i was doing a small mistake.!!! –  miku Oct 4 '13 at 13:08

Javascript Date() instance has different behavior across browsers and not recommended for correct timing entry. It has mixed result between browsers, so even you are using it correctly you may get unexpected results and validating it is difficult. read here

Executing new Date() always returns current local time zone date and time.

However, setting date manually from javascript you can just use toISOString() method of Date API

SO.date__c = new Date().toISOString(); // return ISO 8601 Extended Format date in UTC

Internally it works as sample:

if ( !Date.prototype.toISOString ) {
  ( function() {

    function pad(number) {
      var r = String(number);
      if ( r.length === 1 ) {
        r = '0' + r;
      }
      return r;
    }

    Date.prototype.toISOString = function() {
      return this.getUTCFullYear()
        + '-' + pad( this.getUTCMonth() + 1 )
        + '-' + pad( this.getUTCDate() )
        + 'T' + pad( this.getUTCHours() )
        + ':' + pad( this.getUTCMinutes() )
        + ':' + pad( this.getUTCSeconds() )
        + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
        + 'Z';
    };

  }() );
}
share|improve this answer
    
its not wotking in IE..any solution for IE.? –  miku Oct 10 '13 at 6:12

The client (browser) and server (salesforce) are unlikely to be set to the same timezone. The clean solution to that problem is to avoid it and just use the server timezone.

If you have control over the definition of Sales_Order__c.date__c, why not just set the default value of that field to "TODAY()"? (That is assuming you are creating the object not updating it.)

If not, you can expose WebService method on a controller that sets the date and do a sforce.apex.execute on your button click to call that.

share|improve this answer
2  
Or, they can use the toolkit the way it was designed, and just pass in the date directly. –  sfdcfox Oct 3 '13 at 20:25
    
Good to know... –  Keith C Oct 3 '13 at 20:44

Your datetime is being returned in GMT and you need a Javascript method to convert from GMT to local time. This code sample may be of help:

http://bytes.com/topic/javascript/answers/478296-utc-date-time-local

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.