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
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.
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.
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.
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.
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.
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.
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)
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 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â
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â.