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]);

4 Answers 4


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.

  • I am getting an fault string error when using this.can you help
    – miku
    Oct 4, 2013 at 6:11
  • You also need to include a record ID. The dates are just fine as described in this answer.
    – sfdcfox
    Oct 4, 2013 at 12:07

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';

  }() );
  • its not wotking in IE..any solution for IE.?
    – miku
    Oct 10, 2013 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.

  • 2
    Or, they can use the toolkit the way it was designed, and just pass in the date directly.
    – sfdcfox
    Oct 3, 2013 at 20:25
  • Good to know...
    – Keith C
    Oct 3, 2013 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:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .