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.

I am trying to get the Date () function to work in my Run Code action to no avail. Is this an issue with the JS environment inside of Flow?

Code:

const now = new Date();

console.log(now);

Output:

1970-01-04T00:00:00.000Z

Expected Output:

2024-07-06T02:23:45.506Z

1 Like

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

Thank you for your response. After reading the Run Code Action Limitations, found here, I realized JS date and time functions do not work entirely in Run Code.

I accomplished what I needed to by including the order creation time in the input and used that as my timestamp. Then I was able to calculate what I needed from there.

Input:

query{
order{
tags,
createdAt
}}

Code:

export default function main(input) {

const creationTime = input.order.createdAt;

1 Like

Out of curiosity, did you find a workaround for this? I’d been aware of those limitations but hadn’t run across a use case that was blocked by them until seeing this.

1 Like

Yes I did. What I ended up doing was using createdAt from inside the order
attributes. Since the trigger I have set is the “order created” trigger,
the creation time is virtually the same as what the JavaScript new Date()
time should be.

Query {
order{
createdAt
}
}

JS Code to access it was then just input.order.createdAt

I was then able to use all the other JS date manipulation functions once I
got a real date in there.

Hope that makes sense. If it doesn’t I can post a more detailed response
and code when I get home.

1 Like

This is the intended way to solve it. Unfortunately random dates are a security risk.

1 Like

Nice had a feeling that would be the way to do it. Can’t really think of a case where generating arbitrary time stamps that aren’t linked to dates tied to objects would be necessary.

I should add to this, I was able to get the current day by using the scheduledAt.

query {
getOrderData {
tags
name
id
}
scheduledAt
}

I noticed that the Scheduled At is when it was triggered not the start date This allowed me to get the current day for doing some comparison funny business.

Yes, this is the preferred/supported way.

Thanks for this answer! Did you also find a way to return a date with your run code block? I am trying to compare a date with a metafield of a order but I can not return a date. I tried to return a string but the metafield is a date and I can’t think of a way to convert that.

Examples using scheduledAt here: https://github.com/Shopify/flow-code-examples/blob/main/run-code-examples/schedule-check-scheduled-day/index.js

Thanks Paul for this clarifycation but this is not what I need. I want to know the date so I can match it a metafield in every order to check if it is the same date. My plan was to get values to check if this was the case, then to iterate trough all the orders and check if they match. Only the metafield is a date so in the condition-block it has to be comparred to a date. Thats why I am trying to get a Date out of the run Code block. Is this possible or do you have another solution?

I’m not following your use case. If you use order created as the trigger, you could use the createdAt date on the order (and similar for other triggers)

Hey Abel,

I wasn’t able to return a date but I was able to return a list of orders that the current date matched a tag on the order. Then I was able to iterate over that list and fire admin API call s to do what I needed to.

I haven’t done much digging on metafields but if they are attached to the orders you are feeding in, you might be able use a similar strategy to iterate through and compare, only return the orders that flag as a match

Sorry for my poor explanation. I will try another way. In my company we make cubberts. We use a metafield to set the date that we will deliver it to the customer. I would like a flow that does something (for example, send the customer a message it is coming) if it is the day of delivery or if it is 3 days until delivery. Do you know a way to accomplish this?

You need to be able to query orders by metafield value. That improvement was recently released in the API, but it looks like it doesn’t yet work on date fields. If the field happens to be a text field, then it would be possible. Docs: https://shopify.dev/docs/apps/build/custom-data/metafields/query-by-metafield-value

Hey Paul, I need to pass today’s date so I can see if it’s later than my metaobject’s startDate variable and branch my logic accordingly.

My GraphQL below successfully passes startDate:
{
getMetaobjectEntry {
startDate
}
}

But as soon as I try to include scheduledAt in that GraphQL I get this error:

  • Field ‘scheduledAt’ doesn’t exist on type ‘Query’

  • Cannot query field “scheduledAt” on type “Query”

Since the Javascript Date function is also not an option, what other way can I access today’s date?

I can’t send “X weeks left” reminder emails if I don’t know what day it is :slightly_smiling_face:

You need to pass a date variable that exists in your workflow. I don’t know what trigger you are using, but many of them will have a date the corresponds to “today”

Thanks Paul, yup, it seems so. Below are the 4 triggered Flow nodes in question:

The execution logs show the “Get metaobject entry” node correctly passes the value of start_date to the “Run code” node (last in the chain). I tried your idea of also passing “today” but get these errors:

  • Field ‘today’ doesn’t exist on type ‘Query’

  • Cannot query field “today” on type “Query”.