I want to set Unix timestamp value to datetime field.
string unixDatetime = '1510654220787';
system.debug('!!!unixDatetime='+datetime.newinstance(long.valueOf(unixDateTime)));
string s = string.valueOfGmt(datetime.newinstance(long.valueOf(unixDateTime)));
system.debug('!!!s='+s);
DateTime dtConverted = DateTime.valueOf((datetime.newinstance(long.valueOf(unixDateTime)).format('yyyy-MM-dd HH:mm:ss')));
system.debug('!!!dtConverted='+dtConverted);
Account acc = [Select Id,Fully_Paid_Date__c From Account where id ='00127F000006TNUb'];
acc.Fully_Paid_Date__c=dtConverted;
update acc;
updated record value
Fully Paid Date 14/11/2017 9:10 PM
When I save this value to account object. The time value is stored as less than one hour.
Best Answer
The wording of your issue isn't very clear, but here's what I'm seeing:
Account
, it shows either 2017-11-14 21:10:20 or 2017-11-14 09:10:20 (depending on if you really meant that the time being displayed is 9:10 PM)Account
to be 2017-11-14 10:10:20Looking at the documentation for Datetime, specifically, the description of
Datetime.newInstance(milliseconds)
...The very last line there is important. When you use
Datetime.newInstance(<timestamp>)
, the result is the time in the GMT/UTC zone.Datetimes in Salesforce are always stored as a timestamp in the GMT/UTC time zone. Salesforce uses the locale set in your user profile (or the org's default locale) to automatically adjust the displayed datetime to match your local time zone. Salesforce automatically does the translation in most other cases as well.
Knowing that, you're either in a UTC+1 or UTC+13 time zone. In either case, if you want your
Account
to display the GMT/UTC timestamp as if it were your own local time, you'll need to adjust theDatetime
to your local time.Luckily, that's fairly easy to accomplish.
As for why we need to subtract the offset... That's because Salesforce adds your timezone offset to the GMT/UTC time when displaying it.
Displayed_Time = GMT_Time + Time_Zone_Offset
Since Salesforce assumes (internally) that
DateTime.newInstance(milliseconds)
returns GMT/UTC (and therefore doesn't need automatic adjustment), we need to pass that method the GMT/UTC timestamp that will result in our target datetime being displayed (and therefore automatically adjusted to local time)GMT_Time = Displayed_Time - Time_Zone_Offset