Date formatting failure in Liquid

Loading

In Power Pages (Power Apps Portals), Liquid is a powerful templating language that allows you to format and manipulate various data types, including dates. However, when it comes to date formatting, issues may arise if the syntax is incorrect, the date is in an unexpected format, or certain data types are not compatible with specific formatting methods.

Here is a guide to help you troubleshoot date formatting failures in Liquid.


Common Causes of Date Formatting Failures

  1. Incorrect Date Format String
  2. Invalid Date Object
  3. Unsupported Date Format in Power Pages
  4. Date Object as a String
  5. Missing or Nil Date Value
  6. Timezone and Locale Issues

1. Incorrect Date Format String

Liquid uses the date filter to format dates. The format string inside the filter specifies how the date should be displayed (e.g., year, month, day, etc.). If the format string is incorrect or contains invalid characters, it may cause the date to display improperly or result in a failure.

Example of Correct Date Format String:

{{ product.created_on | date: "%Y-%m-%d" }}

This will format the date as 2025-04-25.

Common Date Format Codes:

  • %Y – 4-digit year (e.g., 2025)
  • %m – 2-digit month (e.g., 04)
  • %d – 2-digit day (e.g., 25)
  • %H – Hour in 24-hour format (00 to 23)
  • %M – Minute (00 to 59)
  • %S – Seconds (00 to 59)

If you mistakenly use an unsupported code or a typo in the format string, the date may fail to render correctly.

Incorrect Example:

{{ product.created_on | date: "%A-%m-%d" }}

%A is not a valid date format specifier in Power Pages.

Fix:

  • Ensure that you are using supported date format codes.
  • Double-check the syntax of the format string.

2. Invalid Date Object

If the variable you’re trying to format is not a valid date object (e.g., it’s a string or null), applying the date filter will not work.

Example:

{{ "2025-04-25" | date: "%Y-%m-%d" }}

This will fail if the string "2025-04-25" is not converted into a date object first.

Fix: Make sure the object you’re trying to format is a valid Date object. If it’s a string, you may need to convert it to a date object first.

Correct Example:

{{ "2025-04-25T00:00:00" | date: "%Y-%m-%d" }}

If the created_on field is stored as a string in Dataverse, ensure it’s returned as a date object.


3. Unsupported Date Format in Power Pages

Power Pages (and Dataverse) may not support all Liquid date format codes or may interpret them differently than expected. For instance, %A (weekday name) or %B (full month name) might not be supported out of the box.

Fix: Stick to the basic date formatting codes for year, month, day, and time (%Y, %m, %d, %H, %M, etc.).

Correct Example:

{{ product.created_on | date: "%Y-%m-%d %H:%M:%S" }}

4. Date Object as a String

If the date is stored as a string in your data source (e.g., Dataverse), and you attempt to format it without converting it to a Date object, it will not work as expected.

Example:

{{ "2025-04-25" | date: "%Y-%m-%d" }}

Here, the string "2025-04-25" must be parsed into a proper Date object.

Fix: If the data is returned as a string, use Liquid’s date_parse filter to convert it into a valid Date object:

{{ "2025-04-25" | date_parse | date: "%Y-%m-%d" }}

This will parse the string into a date object before applying the formatting filter.


5. Missing or Nil Date Value

If the date value is nil or missing, the date filter will fail to format it, resulting in an empty string or an error.

Example:

{{ product.created_on | date: "%Y-%m-%d" }}

If created_on is missing or not set, this will result in an empty string or error.

Fix: Check for a valid date before applying the date filter:

{% if product.created_on %}
{{ product.created_on | date: "%Y-%m-%d" }}
{% else %}
No date available
{% endif %}

This will ensure the filter is only applied if the date exists.


6. Timezone and Locale Issues

Sometimes, date formatting might fail due to timezone or locale differences. Liquid’s date filter uses the server’s timezone by default, which can cause issues if the date object is in a different timezone.

Fix: Ensure that the date is in the correct timezone before formatting. You may need to convert the date to a specific timezone first (which might require additional logic or a custom solution).

For example, if your date is in UTC and needs to be converted to the local timezone, you might need to use JavaScript or a custom solution to convert it before passing it to Liquid for formatting.


Best Practices for Date Formatting in Liquid

  1. Use Supported Date Format Codes: Stick to basic formatting codes (%Y, %m, %d, etc.), and avoid using unsupported codes.
  2. Ensure Proper Date Data Type: Make sure the variable you’re formatting is a valid Date object. Convert strings into date objects if necessary.
  3. Validate Date Existence: Always check if the date exists before formatting it.
  4. Use Caching Carefully: If you’re relying on dynamic data, be aware of potential caching issues that could prevent updated date values from appearing immediately.
  5. Timezone Handling: Ensure your date objects are in the correct timezone before applying the filter.

Example Code

{% if product.created_on %}
{{ product.created_on | date: "%Y-%m-%d" }}
{% else %}
Date not available.
{% endif %}

This checks if the created_on field is available and then formats it. If the date is not available, it outputs a default message.


Troubleshooting Summary Table

IssueCauseFix
Incorrect format stringInvalid or unsupported date format codeUse valid date format codes like %Y, %m, %d
Invalid date objectString or nil instead of Date objectEnsure the object is a valid Date (use date_parse)
Missing date valueNil or missing date fieldUse {% if %} to check for a valid date before applying
Unsupported format in Power PagesUsing unsupported format codesStick to supported codes and basic formatting options
Timezone issuesIncorrect timezone in date objectConvert date to the desired timezone before formatting

Leave a Reply

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