How to Get Current Time Inside a Shopify Function (Rust / JavaScript)?

Hi everyone,

We’re working on a Shopify Function and need to compare the current date & time with values stored in metafields. However, Shopify Functions don’t seem to allow direct access to real-time system clocks the way normal apps do.

Is there a recommended way to get the current time inside a Shopify Function (Rust or JavaScript)?
For example, we want to check whether a discount or rule is active based on a start/end datetime saved in metafields.

I understand that Functions run in a restricted environment with no external APIs or system calls. So:

  • Is there any built-in method or environment variable for getting the current UTC timestamp?

  • Do we need to pass the current time from the storefront/cart to the function input?

  • Or is there another best-practice approach for time-based conditions?

Any guidance or examples (Rust or JS) would be really helpful.

Thanks!

Short answer: Shopify Functions do not reliably provide a process-level system clock you can call from new Date() inside the sandboxed Function runtime, so the recommended approaches are (A) use the shop.localTime helpers that Shopify exposes in the Function run input (the LocalTime/dateTime-style fields or dateTimeBefore/dateTimeAfter helpers when available) to evaluate time-based conditions, or (B) if your Function API version / runtime does not include a date-time value you need, pass the current timestamp into the Function as part of the input (for example from your storefront or from your app server) and compare that against the datetime stored in metafields — many community posts and Shopify guidance show new Date() in the Function/runtime returning epoch/invalid times, and Shopify has added LocalTime/dateTime input to address this exact use case. (Stack Overflow)

Practical guidance you can use immediately: 1) inspect the Function run input your Function receives (GraphQL schema for the trigger) and look for shop { localTime { date, dateTimeBefore(...), dateTimeAfter(...), ... } } — if dateTime or the dateTimeBefore/dateTimeAfter helpers exist for your API version you should use those server-provided values to compare against your metafield datetimes (this keeps everything inside the Function and avoids external calls). (Shopify) 2) if the run input does not include a suitable timestamp (or you need an exact UTC instant), have your storefront or app server inject the current ISO 8601 timestamp into the Function input (for cart/checkout flows you can add it to the GraphQL variables or cart attributes that the Function receives) and then compare that string to the metafield value inside your Function. Community solutions for Run Code / Flow and Functions commonly use trusted timestamps from an upstream object (order.createdAt, trigger scheduledAt) or pass current time from the client because new Date() in the sandbox is unreliable. (Shopify Community)

Tiny examples (conceptual, so you can adapt to Rust or JS):

  • GraphQL input snippet (what to request in run input):
shop {
  localTime {
    dateTime       # if available — server-provided ISO timestamp
    dateTimeBefore(dateTime: "2025-11-24T18:00:00Z")  # helper example
  }
}
# plus metafields you need to read, e.g. shop.metafield(...) or cart.metafields
  • JS pseudo-logic inside a Function (when you receive shop.localTime.dateTime or a passed now):
const nowIso = input.shop?.localTime?.dateTime ?? input.runtimeNowIso; // prefer shop.localTime
const now = new Date(nowIso);
const start = new Date(metafieldStartIso);
const end = new Date(metafieldEndIso);

if (now >= start && now <= end) {
  // rule active
} else {
  // inactive
}
  • Rust pseudo-logic (using chrono-style ISO parsing) — same idea: parse the ISO string you received from shop.localTime.dateTime or your injected now and compare with parsed metafield values.

Key takeaways: don’t rely on new Date() inside the Function runtime; first look for shop.localTime / dateTime in the run input and use built-in dateTimeBefore/dateTimeAfter helpers if present; otherwise, pass a trusted ISO timestamp into the Function (from storefront or app server or from an upstream object such as order.createdAt) and compare that to your metafield datetimes. If you want, tell me which Function type (discount, cart validation, shipping) and which API version you’re targeting and I’ll give copy-paste-ready JS and Rust snippets and the exact run-input GraphQL you should query for that API version.

Ran into a similar problem with Checkout UI Extension. I ended up having a Shop datetime metafield, which is updated by Flow couple of times a day.

You could use the same premise here too, the only downside is it would only be accurate to every 10 minutes as the fastest Flow can run is every 10 minutes.