Re: Liquid - get last day of the month using a date

Liquid - get last day of the month using a date

FBG_1211
Visitor
2 0 0

Hi to all

 

for my customer requirement I need to get the last day of the month starting from a given date

For example : I have 

date = '2027-11-01' 

I need to get endOfMonth='2027-11-30'

 

Any idea on how to get that result using liquid ?

 

Thanks

Replies 3 (3)

Small_Task_Help
Shopify Partner
778 25 69

Hi,

 

Converting the given date to a Liquid date object and then using the date filter to get the last day of the month.

{% assign date = '2027-11-01' | date: "%Y-%m-%d" %}
{% assign endOfMonth = date | date: "%Y-%m-%d" | date_add: '1m' | date: "%Y-%m-01" | date_subtract: '1d' %}

End of Month: {{ endOfMonth }}
To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
FBG_1211
Visitor
2 0 0

Hello

Thanks but I got an error => Liquid error: undefined filter date_add

In some use case my date can be for example '2027-11-16' and I need to get from that date '2027-11-30'

Small_Task_Help
Shopify Partner
778 25 69

Need to check from our end. If you want you can contact us (details given at signature)

To Get Shopify Experts Help, E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert Agency
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad