Home > Shopify Development > Set a Date in Any Format

How to Set a Date in Any Format - A How-to Guide

Sam Nguyen
Sam Updated: February 17, 2024

Share:

Drive 20-40% of your revenue with Avada
avada email marketing

As you are merchant, you might think that formatting dates can become tricky if you require a specific format. However, now, you should not be worried about that since in this article, you will be shown how to set a date in any format.

Related Posts:

How to set a date in any format

Step 1: Set a date in ISO 8601 format

You can start setting a date in ISO 8601 format. Them you can get dates through a filter to receive the desired format.

---
layout: default

title: Date Formatting

date: 2021-03-23T10:20:00Z

---

Step 2: Set up the date formatting {#set-up-date-formatting)

  • With date_to_long_string, you can input like the following structure:

{{ page.date | date_to_long_string }}

The output will be like: 23 March 2021

  • With date_to_rfc822, you can input like the following structure:

{{ page.date | date_to_rfc822 }}

The output will be like: Wed, 23 Mar 2021 23:20:00 +1300

  • With date_to_string, you can input like the following structure:

{{ page.date | date_to_string }}

The output will be like: 23 Mar 2021

  • With date_to_xmlschema, you can input like the following structure:

{{ page.date | date_to_xmlschema }}

The output will be like: 2021-03-23T23:20:00+13:00

Date will give you a complete format and you can have the rights to control it. Take note that you can specify the template of format you want. For instance:

  • With Date, you can input like the following format:

{{ page.date | date: "%m/%d/%Y" }}

The outtput will be like: 03/23/2021

  • Or, you can input like this:

{{ page.date | date: "%-d %B %Y"}}

The output will be like: 23 March 2021

Here are a table of many placeholders we can use for date formatting:

Placeholder Format Example
%a Abbreviated weekday Sun
%A Full weekday name Sunday
%b Abbreviated month name Jan
%B Full month name January
%c Preferred local date and time representation Fri Jan 29 11:16:09 2021
%d Day of the month, zero-padded 05
%-d Day of the month 5
%D Formats the date 29/01/16
%e Day of the month 3
%F Returns the date in ISO 8601 format 2021-01-29
%H Hour of the day, 24-hour clock 07
%I Hour of the day, 12-hour clock 04
%j Day of the year 017
%k Hour of the day, 24-hour clock 7
%m Month of the year 04
%M Minute of the hour 09
%p Meridian indicator uppercase AM
%P Meridian indicator lowercase pm
%r 12-hour time 01:31:43 PM
%R 24-hour time 18:09
%T 24-hour time with seconds 18:09:13
%s Number of seconds since 1970-01-01 00:00:00 UTC 1452355261
%S Second of the minute 05
%U Week number of the current year, starting with the first Sunday as the first day of the first week 03
%W Week number of the current year, starting with the first Monday as the first day of the first week 09
%w Day of the week. Sunday is 0 4
%x Preferred representation for the date 05/11/15
%X Preferred representation for the time 17:15:31
%y Year without a century 16
%Y Year with a century 2021
%Z Time zone name PST
%% Literal % character %

Step 3: Sep up ordinal dates

One thing that you should bear in mind you might not get the ordinal date. For instance, merchants cannot output something like May 23rd since there is no placeholder setting up for rd. And there is a solution for it that you can use Liquid in order to calculate as well as output the ordinal date.

You can input the following structure like that:

{% assign day = page.date | date: "%-d"  %}
{% case day %}
  {% when '1' or '21' or '31' %}{{ day }}st
  {% when '2' or '22' %}{{ day }}nd
  {% when '3' or '23' %}{{ day }}rd
  {% else %}{{ day }}th
{% endcase %}
{{ page.date | date: "of %B, %Y" }}

The output will be like: 23rd of March, 2021

Conclusion

We hope that the article on how to set a date in any format will help you set up your date like you want and to make your site become more professional. For more article like this, check out our posts on catalog or Shopify Custom App Development Tutorials.


Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.

People also searched for

Stay in the know

Get special offers on the latest news from AVADA.