Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
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
Solved! Go to the solution
This is an accepted 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.
Input:
query{
order{
tags,
createdAt
}}
Code:
export default function main(input) {
const creationTime = input.order.createdAt;
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.
If our suggestions are useful, please let us know by giving it a like, marking it as a solution.
MIDA: Heatmap, Record & Replay |BLOOP Referral Program, Reward |
Need help from our expert? Kindly share your request with us via community@bsscommerce.com
This is an accepted 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.
Input:
query{
order{
tags,
createdAt
}}
Code:
export default function main(input) {
const creationTime = input.order.createdAt;
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.
This is the intended way to solve it. Unfortunately random dates are a security risk.
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.
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:
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 🙂
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:
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-d...
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)
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 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
Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025