Blog

Salesforce Date & Time Patterns: Guide & Examples

Salesforce provides powerful tools for managing and manipulating data, including date and time formats. Proper date and time formatting is essential for accurate data representation, especially in environments where data consistency and readability are crucial. This blog post will explore the various date and time pattern strings used in Salesforce, helping you to understand and apply them effectively.

Date and Time Pattern Strings

Pattern strings specify Date and time formats in Salesforce. These strings use unquoted letters from ‘A’ to ‘Z’ and from ‘a’ to ‘z’ as pattern letters representing different components of a date or time string. Any other characters included in the string are not interpreted; they are copied into the output string during formatting or matched against the input string during parsing.

Here is a comprehensive list of the pattern letters and their corresponding date or time components, presentation, and examples :

How to Use These Patterns

When defining a date format in Salesforce, you combine these pattern letters to create a custom date and time string. For instance, if you want to format a date to include the full year, month, and day, along with the time in hours, minutes, and seconds, you might use the pattern yyyy-MM-dd HH:mm:ss. This pattern ensures that your date will be displayed as 2023-06-13 14:30:45.

Practical Examples :

1. Formatting a Date for Reports

  • Pattern: yyyy-MM-dd
  • Example: 2024-06-13
  • Use Case: This format is useful for generating clear and consistent dates in reports.

2. Displaying User-Friendly Date and Time

  • Pattern: E, MMM dd yyyy ‘at’ h:mm a
  • Example: Tue, Jun 13 2024 at 2:30 PM
  • Use Case: This pattern provides a more readable date and time format, which can be particularly helpful for user interfaces.

3. Full Date with Time

  • Pattern: yyyy-MM-dd’T’HH:mm:ss.SSSZ
  • Example: 2024-06-13T14:30:45.123-0800
  • Use Case: This ISO 8601 format is ideal for storing precise timestamps in a database.

4. Full Date with Time

  • Pattern: yyyy-MM-dd HH:mm:ss
  • Example: 2024-06-13 14:30:45
  • Use Case: Storing complete date and time information.

5. Short Date

  • Pattern: MM/dd/yyyy
  • Example: 06/13/2024
  • Use Case: Commonly used in the US for general date representation.

6. Long Date

  • Pattern: EEEE, MMMM d, yyyy
  • Example: Tuesday, June 13, 2024
  • Use Case: Displaying a full date in a user-friendly format.

7. Day and Month

  • Pattern: d MMMM
  • Example: 13 June
  • Use Case: Showing day and month without year, often used in event reminders.

8. Year and Month

  • Pattern: MMMM yyyy
  • Example: June 2024
  • Use Case: Representing a specific month in a particular year, useful for billing cycles.

9. Day, Month, and Year

  • Pattern: dd MMM yyyy
  • Example: 13 Jun 2024
  • Use Case: Concise date format used in many international contexts.

10. Time Only (24-hour format)

  • Pattern: HH:mm
  • Example: 14:30
  • Use Case: Showing time in a 24-hour clock format.

11. Time Only (12-hour format with AM/PM)

  • Pattern: h:mm a
  • Example: 2:30 PM
  • Use Case: Displaying time in a 12-hour clock format.

12. Full Date with Day of the Week

  • Pattern: EEEE, dd MMMM yyyy
  • Example: Tuesday, 13 June 2024
  • Use Case: Providing a complete date including the day of the week, often used in schedules and invitations.

13. ISO 8601 Date and Time

  • Pattern: yyyy-MM-dd’T’HH:mm:ss.SSSZ
  • Example: 2024-06-13T14:30:45.123-0800
  • Use Case: Storing timestamps in a standardized format, suitable for APIs and databases.

14. Week Year and Week Number

  • Pattern: ‘Week’ w ‘of’ YYYY
  • Example: Week 24 of 2024
  • Use Case: Representing specific weeks within a year, useful in project planning.

15. Day of the Week and Time

  • Pattern: E, h:mm a
  • Example: Tue, 2:30 PM
  • Use Case: Displaying a specific time on a given day of the week.

16. Day and Time with Seconds

  • Pattern: E, MMM dd, yyyy h:mm:ss a
  • Example: Tue, Jun 13, 2024 2:30:45 PM
  • Use Case: Showing precise time down to seconds, often used in logging events.

17. Time with Time Zone

  • Pattern: HH:mm:ss z
  • Example: 14:30:45 PST
  • Use Case: Representing time with time zone information.

18. RFC 822 Time Zone

  • Pattern: Z
  • Example: -0800
  • Use Case: Time zone representation in RFC 822 format, often used in email headers

19. ISO 8601 Time Zone

  • Pattern: X
  • Example: -08:00
  • Use Case: Time zone representation in ISO 8601 format, suitable for web standards.

20. Calendar Year

  • Pattern: dd/MM/yyyy
  • Example: 13/06/2024
  • Use Case:  Represents the calendar year, which runs from January 1 to December 31.

21.  ISO Week Year

  • Pattern: dd/MM/YYYY
  • Example: 13/06/2024
  • Use Case: Represents the ISO week year, which may start in the previous calendar year or extend into the next calendar year. This is important for weeks that overlap years, such as the last week of December or the first week of January.

By using these patterns, you can format dates and times in Salesforce to fit a variety of needs, ensuring your data is both clear and appropriately detailed. Whether you need a simple date for a report or a full timestamp for logging, these patterns will help you get the job done.

Sample Use Case :

For sample use case we picked up Calendar Year(dd/MM/yyyy) format and ISO Week Year(dd/MM/YYYY) format.

We have stamped the datetime value as 1998-12-31 00:00:00

Whereas,

  • The calendar year format results in 31/12/1998
  • The ISO Week Year format results in 31/12/1999

Complying with local, national, and international standards and accurately representing data depend on knowing the distinctions between the dd/MM/yyyy and dd/MM/YYYY formats. Regardless of whether you work with reporting, user interfaces, financial planning, or compliance documents, selecting the appropriate date format for your Salesforce environment guarantees uniformity and clarity. To better satisfy your unique business requirements and improve data accuracy throughout all of your Salesforce apps, use these formats as applicable.

Certainly! Here are various date and time formats, each with its corresponding pattern, examples, via salesforce :

Results :

Leave a Reply

Your email address will not be published. Required fields are marked *