JavaScript Date/Time functions not working in Run Code Action

Solved

JavaScript Date/Time functions not working in Run Code Action

craiganderson
Shopify Partner
10 2 8

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
10 2 8

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 6 (6)

BSSCommerce-TC
Shopify Expert
168 36 36

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
10 2 8

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
521 17 76

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
10 2 8
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
1150 142 273

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
521 17 76

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.