Apex DateTime Regional Timezone Conversion Interoperability
0 reputation · 25 Feb 2023, 16:39 UTC
0 reputation · 25 Feb 2023, 16:39 UTC
Apex DateTime values are inherently stored in UTC.
Conversion to a specific regional time zone relies on the organizational or user default context.
This context may not accurately reflect an arbitrary external regional requirement.
37275 reputation · 26 Feb 2023, 02:11 UTC
Yes. DateTime.format(String formatString, String timezone) accepts an explicit IANA zone identifier (for example, 'America/Los_Angeles' or 'Europe/Paris') as its second argument. When provided, this overrides the running user’s or org’s default timezone. The platform’s bundled tzdata applies daylight-saving transitions automatically—no manual offset calculation is required.
String format(String formatString, String timezone) — the timezone parameter takes any IANA zone ID present in the Salesforce runtime’s tzdata.EDT vs. EST) without additional code.null, the method uses the context user’s timezone setting—the source of the “regional conversion ambiguity” you described.DateTime.now().format('yyyy-MM-dd HH:mm:ss z', 'America/New_York') (or any valid IANA ID) to produce a string in that zone.try/catch for System.TypeException—an invalid zone ID throws at runtime.DateTime instance in the target zone for further calculation, parse the formatted string back via DateTime.parse() or perform arithmetic on the original UTC value; format() returns a String, not a shifted DateTime.tzdata version per release is not exposed by Salesforce. A zone whose rules changed very recently may behave differently until the next platform update.format() produces locale-dependent month names and AM/PM markers based on the running user’s locale, not the target timezone’s locale.format() with varying zones can increase CPU time; consider caching or bulk conversion.System.debug(Datetime.now().format('yyyy-MM-dd HH:mm:ss z', 'America/New_York'));Execute this in Anonymous Apex across a known DST boundary (e.g., early November and early March) and observe the EDT/EST suffix change automatically.
What is the API version of the Apex class or trigger where this code runs? If it is below v60.0, IANA zone support is not guaranteed and you may need to upgrade the class version or use a supported Java timezone ID instead.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.