Beyond Exact Dates: Fetching Shopify Orders from Relative Timeframes

Hey everyone! As a Shopify migration expert and someone who loves digging into what makes store owners tick, I often see recurring questions in the community that really highlight common pain points. A fantastic recent thread, started by our community member ctevan, perfectly illustrates one of these: how do you pull data, specifically draft orders, from a relative timeframe? Think "orders from 7 to 14 days ago" instead of "orders between January 1st and January 8th."

It's a common scenario, right? You might need to follow up on abandoned carts from a specific window, analyze recent sales trends, or trigger automations based on orders that haven't been touched in a week. The challenge ctevan brought up was that most examples for created_at queries use exact timestamps, making relative queries seem tricky. But good news! Our community came through with some stellar solutions.

The Challenge: Relative Dates in Shopify Queries

When you're working with Shopify's powerful data, whether through an app, custom code, or Shopify Flow, querying by specific dates is straightforward. You just plug in your start and end dates. But what if your "start" and "end" dates are constantly shifting, like "two weeks ago" or "yesterday"? Manually updating those timestamps every time is a pain, and frankly, it's not scalable.

ctevan was looking to grab all draft orders from "2 weeks ago, so between 7 and 14 days ago." This isn't just about getting data; it's about automating a dynamic process. Let's dive into the brilliant ways the community tackled this.

Solution 1: Shopify Flow & Liquid for Automated Queries

One of the most powerful tools for store owners looking to automate tasks without deep coding knowledge is Shopify Flow. And as ecom-4all pointed out in the thread, Flow's "Get data" action is incredibly flexible because its query field understands Liquid.

How to Use Liquid's date_minus Filter in Shopify Flow

The key here is Liquid's date_minus filter. This little gem allows you to dynamically calculate a date by subtracting a specified duration from a given date (like "now").

Step-by-Step Instructions for Shopify Flow:

  1. Set up your Flow Trigger: Decide when you want this query to run. For example, you might set a "Scheduled time" trigger to run daily, or after a specific event.
  2. Add a "Get data" Action: In your Flow, add an action. Search for "Get data" and choose the relevant data type (e.g., "Get orders" or "Get draft orders").
  3. Craft your Query using Liquid: In the "Query" field of the "Get data" action, you'll use Liquid to construct your dynamic date range. Here's the exact snippet ecom-4all provided, which is perfect for getting orders between 7 and 14 days ago:
    created_at:>='{{ "now" | date_minus: "14 days" | date: "%Y-%m-%dT%H:%M:%SZ" }}' created_at:<='{{ "now" | date_minus: "7 days" | date: "%Y-%m-%dT%H:%M:%SZ" }}'
    

    Let's break that down a bit:

    • "now": This literally means the current timestamp when the Flow runs.
    • | date_minus: "14 days": This filter subtracts 14 days from "now". You can use various formats like "14 days", a number of seconds, or an ISO 8601 duration.
    • | date: "%Y-%m-%dT%H:%M:%SZ": This formats the resulting date into the ISO 8601 string format that Shopify's API queries expect.
    • created_at:>= and created_at:<=: These define your "greater than or equal to" and "less than or equal to" range for the created_at field.
  4. Test and Verify: Before you point this at anything critical, ecom-4all wisely advises to run the Flow once and then open the run log for that step. The log will print the query after Liquid has resolved the dates, so you can confirm the timestamps are exactly what you expect (e.g., 14 and 7 days back from the run time). This is a crucial step for debugging!

This method is fantastic for recurring internal tasks and automating processes right within Shopify.

Solution 2: GraphQL API for Developers & Custom Integrations

For those of you building custom apps, integrations, or working directly with the Shopify API, thestoremechanic offered excellent GraphQL solutions. This approach gives you more control and is ideal for external systems or more complex data operations.

Method A: Direct Relative Dates in GraphQL Query

Shopify's GraphQL API search syntax is surprisingly smart! It actually accepts relative date values directly for fields like created_at.

GraphQL Query Example:

To get draft orders from 7 to 14 days ago, you can use this concise query argument:

query: "created_at:>=-14d created_at:<=-7d"

This is incredibly clean and readable. The -14d simply means "14 days ago" from the current moment the query is executed. This is often the simplest approach if your API client allows for direct string query arguments.

Method B: Calculate ISO Timestamps in Your Code

If you need more precision, are working with a language that makes date calculations easy, or want to ensure your exact timestamps are passed, you can calculate the ISO timestamps in your code before sending the GraphQL query. This is particularly useful if your relative time logic is more complex than simple "X days ago."

JavaScript Example for Calculating Timestamps:

const min = new Date(Date.now() - 14*864e5).toISOString();
const max = new Date(Date.now() - 7*864e5).toISOString();
// Then use these in your GraphQL query string:
// query: `created_at:>='${min}' created_at:<='${max}'`

In this JavaScript snippet:

  • Date.now() gives you the current timestamp in milliseconds.
  • 14*864e5 calculates 14 days in milliseconds (1 day = 86,400,000 ms, which is 864e5).
  • new Date(...).toISOString() converts that millisecond timestamp into the required ISO 8601 string format.

You would then embed these min and max variables into your GraphQL query string, just as thestoremechanic illustrated.

Choosing Your Approach: Flow vs. GraphQL

Both solutions are valid and powerful, but they cater to different needs:

  • Shopify Flow with Liquid is perfect for store owners and operations managers who want to automate tasks directly within their Shopify admin. It's less code-intensive, highly visual, and great for recurring internal processes.
  • GraphQL API with relative dates or calculated timestamps is ideal for developers, custom app builders, and more complex integrations. It offers greater flexibility, control, and is suitable for connecting Shopify data to external dashboards, CRMs, or other business systems.

Regardless of which method you choose, the key takeaway from ctevan's question and the community's fantastic answers is that Shopify provides flexible ways to query your data dynamically. No more manual date calculations! Always remember to test your queries, especially when dealing with dynamic dates, to ensure you're getting exactly the data you need. This kind of automation can save you hours and ensure your data-driven decisions are always based on the most relevant information.

Share:

Use cases

Explore use cases

Agencies, store owners, enterprise — find the migration path that fits.

Explore use cases