Skip to main content
CRMSalesforce CRM

Format Salesforce DateTime as Strings with formatted Time

By March 24, 20202 Comments

There are times when you want to format a DateTime field with a standard format for the Time.  For example:  3/25/20 13:30 as 3/25/20 at 1:30 pm.  This can be useful in templates for reminders to give it less of a ‘form letter’ feel.

The formula below can be used in several ways.  They can be used to update a field in a Process Builder, used in a flow, or most places a formula can be used to manipulate data.

In the example below I have a flow that runs every day that looks for events coming up in the following week.  Those event records have a custom field that updates with the DateTime String so it can be used in a reminder template.

To format the Time in a datetime field we first find the Hour and convert from military time to standard time:

TEXT(IF( OR( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) = 0, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24  ), 12, 2 ) ) = 12 ), 12, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) – IF( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) < 12, 0, 12 ) ))

NOTE:  In the formula(s) the value 5/24 is the time zone offset value.  5 represent Central Standard Time (GMT – 5).  Update this value accordingly.

Please see the Accounting for Daylight Savings Time in Salesforce Tip and Trick for more information on maintaining these.

Next we find the value for the Minutes:

MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24 ), 15, 2 )

Finally we evaluate if the time is AM or PM:

IF( VALUE( MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24), 12, 2 ) ) < 12, “am”, “pm” )

Once you have the date formatted as you like, you can add text to complete the string for your needs:

TEXT(DATE({!ActivityDateTime.ActivityDateTime} )) & ” at ” & TEXT(IF( OR( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) = 0, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24  ), 12, 2 ) ) = 12 ), 12, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) – IF( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) < 12, 0, 12 ) )) & “:” & MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24 ), 15, 2 ) & ” ” & IF( VALUE( MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24), 12, 2 ) ) < 12, “am”, “pm” )

The above formula with display as:  3/25/20 at 1:30 pm

By adding the formula from the Format Salesforce Dates as Strings with Month/Day Names Tip and Trick you can fully format the DateTime field into a string:

“On ” & CASE( MOD( {!ActivityDateTime.ActivityDate} – DATE(1900, 1, 7), 7), 0, “Sunday”, 1, “Monday”, 2, “Tuesday”, 3, “Wednesday”, 4, “Thursday”, 5, “Friday”, 6, “Saturday”, “Error”) & “, ” &

CASE(MONTH({!ActivityDateTime.ActivityDate}),

1, “January”,

2, “February”,

3, “March”,

4, “April”,

5, “May”,

6, “June”,

7, “July”,

8, “August”,

9, “September”,

10, “October”,

11, “November”,

12, “December”,

“None”

)& ” ” &

TEXT(DAY( {!ActivityDateTime.ActivityDate})) & “, ” & TEXT(YEAR({!ActivityDateTime.ActivityDate}))

& ” at ” & TEXT(IF( OR( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) = 0, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24  ), 12, 2 ) ) = 12 ), 12, VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) – IF( VALUE( MID( TEXT( {!ActivityDateTime.ActivityDateTime} – 5/24 ), 12, 2 ) ) < 12, 0, 12 ) )) & “:” & MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24 ), 15, 2 ) & ” ” & IF( VALUE( MID( TEXT(  {!ActivityDateTime.ActivityDateTime} – 5/24), 12, 2 ) ) < 12, “am”, “pm” )

The above formula with display as:  On Wednesday, March 25, 2020 at 1:30 pm

Becci MG

Author Becci MG

More posts by Becci MG

Join the discussion 2 Comments

  • Martina says:

    Hi, I’m using this in the process builder, with [Task].Start_Date_Time__c as an inserted field in the formula. However, I get a syntax error. Any idea what’s going wrong?

    TEXT(DAY([Task].Start_Date_Time__c )) & “, ” & TEXT(YEAR([Task].Start_Date_Time__c ))
    & ” at ” & TEXT(IF( OR( VALUE( MID( TEXT( [Task].Start_Date_Time__c – 5/24 ), 12, 2 ) ) = 0, VALUE( MID( TEXT([Task].Start_Date_Time__c – 5/24 ), 12, 2 ) ) = 12 ), 12, VALUE( MID( TEXT( [Task].Start_Date_Time__c – 5/24 ), 12, 2 ) ) – IF( VALUE( MID( TEXT( [Task].Start_Date_Time__c – 5/24 ), 12, 2 ) ) < 12, 0, 12 ) )) & “:” & MID( TEXT( [Task].Start_Date_Time__c – 5/24 ), 15, 2 ) & ” ” & IF( VALUE( MID( TEXT( [Task].Start_Date_Time__c – 5/24), 12, 2 ) ) < 12, “am”, “pm” )

    • Becci says:

      Hi Martina,

      What format are you trying to create? From your example it is not the 3/25/20 at 1:30 pm format discussed. If you are wanting the post format, try changing the field name but be sure to use Salesforce to get the proper syntax (e.g., you may be missing something). If you are looking for a different format, send it over and I will dive deeper. Also send any specifics you may have gotten with the syntax error.

      Becci

Leave a Reply

5 Great Alternatives to SalesForce.com

Free Whitepaper Download

Learn more about 5 options that are better CRMs for small business

DOWNLOAD WHITEPAPER

Skip to content