JavaScript Date/Time functions not working in Run Code Action

Topic summary

Core Issue:
JavaScript’s new Date() function returns incorrect timestamps (e.g., 1970-01-04) when used in Shopify Flow’s Run Code action due to security limitations that prevent generating arbitrary dates.

Primary Solution:
Use existing date fields from Shopify objects instead of creating new dates:

  • For order-based flows: Query createdAt from the order object
  • For scheduled flows: Use scheduledAt from the workflow trigger
  • Access via GraphQL query and reference in code as input.order.createdAt

Key Limitations:

  • Random date generation is intentionally blocked for security
  • Date manipulation functions work once a real date is obtained from Shopify objects
  • Querying orders by metafield date values is not yet supported in the API (only text fields currently work)

Open Challenges:

  • Comparing current dates with order metafield dates remains difficult
  • No direct way to access “today’s date” without a trigger-provided timestamp
  • Users seeking to send delivery reminders based on metafield dates lack a clear solution

Resources:
Shopify provides code examples using scheduledAt in their Flow examples repository.

Summarized with AI on October 31. AI used: claude-sonnet-4-5-20250929.

Hi @craiganderson ,

What environment are you running? You can use the code below to check the environment’s timezone.

const now = new Date();

// Time as an ISO string
console.log("Time ISO " + now.toISOString());

// timezone offset
console.log("Timezone Offset: " + now.getTimezoneOffset());

// time in the local timezone
console.log("LocalTime: " + now.toLocaleString());

==================================================

Result:
Time ISO: 2024-07-06T03:52:20.911Z
Timezone Offset: -420
Local Time: 7/6/2024, 10:52:20 AM

If it’s helpful with you, please give me a like and mark it as a solution.

Thanks in advance.

1 Like