I know there a lot of answers on the web about this issue. I don't know why or what I do wrong, I cant achieve my need.

I'm getting a dateTime from a web service (Guest user) in this format :

05/10/2020 01:05 - MM\DD\YYYY HH:MM

I want to save it in salesforce in a DateTime field exactly as its received - 05/10/2020 01:05:00.

But when I save it, The GMT has come to action and add me 3 hours extra.

I just want to remove the GMT option - I want to receive the date and time as it is.

I tried :

Datetime StartDT = Datetime.newInstance(year,month,day,hour,min,sec);
string modStartDT = StartDT.format('yyyy-MM-dd\'T\'HH:mm:ss','GMT'); 


 String stringDate = dateTimeAsString.substring(6,10) + '-' + 
 dateTimeAsString.substring(0,2) + '-' +
 dateTimeAsString.substring(3,5) + ' ' +

 DateTime dt = datetime.valueOf(stringDate);

I tried to add my user GMT in the TimeZone Object.

But what I do not understand - is that guest Users have the GMT Time zone anyway.

Why is it adding the extra hour anyway?

every help will be much appreciated.


Salesforce interprets Datetime values in the context of the user's timezone. These values are stored in UTC within the database (this is like a timezone with zero offset, and the timezone with zero offset is GMT which is, I guess, why Salesforce call it GMT). Importantly it includes no DST transitions either.

If you want to "parse" a string received without any timezone offsetting based on the user timezone selection use Datetime.valueOfGmt or pre-parse it into component parts and use Datetime.newInstanceGmt.

When outputting the value again you'll want to use Datetime.formatGmt.

Take a look at the documentation for more.

  • "user's locale: should be "user's time zone setting". They are two different things.
    – sfdcfox
    Mar 19 '20 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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