Skip to main content

Timezones and formulas

Salesforce formulas are a bit bogus regarding timezones. Which makes things tricky with Flows, when you want to only display the time of a datetime. E.g.:

  • outputting a datetime field in a text template shows it in the proper timezone. Like: 21/11/2019 14:40
  • the HOUR() function does not work in Flows, so I'm trying to use dirty string manipulations. But the exact same field as above, once wrapped in a TEXT(), outputs "2019-11-21 12:40:00Z", which does not account for the timezone difference... difference there is no simple way to retrieve in formulas. This interesting discussion https://success.salesforce.com/answers?id=9063A000000suLPQAY gives some interesting solutions, though, but leading to convoluted formulas

I know this is not a Flow team problem per se, but since Flow is obviously getting a lot of love recently, and is doing great at going no-code, is there any chance the TEXT() function bug is fixed, by gentle pressure from the Flow team 😇?

@Shannon Hale 

3 条评论
  1. 2023年12月2日 02:32

    If you're trying to set "PM" or "AM" how do you conditionally do that in the datetime value that you extract? With DST?

     

    Apex is likely the most consistent solution here.

     

    public class GetCurrentDateTimeInPST {

     

        @InvocableMethod(label='Get Current Date Time in PST' description='Returns the current date and time in PST')

        public static List<DateTime> getCurrentDateTimeInPST() {

            // Get the current date and time in UTC

            DateTime nowUTC = DateTime.now();

     

            // Format the current DateTime to a string in PST

            String formattedPST = nowUTC.format('yyyy-MM-dd HH:mm:ss', 'America/Los_Angeles');

     

            // Convert the formatted string back to DateTime without timezone adjustment

            DateTime nowPST = DateTime.valueOf(formattedPST);

            

            // Return the result

            return new List<DateTime>{ nowPST };

        }

    }