JavaScript Date/Time functions not working in Run Code Action

Solved

JavaScript Date/Time functions not working in Run Code Action

craiganderson
Shopify Partner
15 2 13

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

Accepted Solution (1)
craiganderson
Shopify Partner
15 2 13

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;

 

View solution in original post

Replies 18 (18)

BSSCommerce-TC
Shopify Partner
225 49 51

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


BSS Commerce - Full-service eCommerce Agency
craiganderson
Shopify Partner
15 2 13

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;

 

Kalen_Jordan
Shopify Partner
803 39 147

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.

craiganderson
Shopify Partner
15 2 13
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.
paul_n
Shopify Staff
1686 185 386

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

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Kalen_Jordan
Shopify Partner
803 39 147

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.

 

Conrad-Barczyk
Shopify Partner
3 0 0

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.

paul_n
Shopify Staff
1686 185 386

Yes, this is the preferred/supported way. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
ReneRafael
Excursionist
12 0 3

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 🙂

paul_n
Shopify Staff
1686 185 386

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"

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
ReneRafael
Excursionist
12 0 3

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

 

Flow2.jpg

 

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".
Abel_Franciscus
Tourist
4 0 0

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.

paul_n
Shopify Staff
1686 185 386

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

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Abel_Franciscus
Tourist
4 0 0

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?

 

paul_n
Shopify Staff
1686 185 386

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)

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Abel_Franciscus
Tourist
4 0 0

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?

paul_n
Shopify Staff
1686 185 386

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

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Conrad-Barczyk
Shopify Partner
3 0 0

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