Search code examples
javascriptshopifyshopify-app

Javascript new Date(); returns 1970-01-01 in Shopify Function


I am working on a Shopify Function in which today's date is compared to another date.

When I run new Date(); to get today's date, it returns "1970-01-01"

I know this is epoch time, but the issue I am having is everyone says new Date(); is supposed to return today's date, but it isn't working for me. Is this an issue with how Shopify extensions handle dateTime parsing, or is there some other way I am supposed to get today's date.


Solution

  • Ok, so I found a solution for this that is specifically related to Shopify. I am creating an extension for Shopify using the Storefront API. To get the current date (dateTime functionality has unfortunately not been added as of writing this answer) you need to add the following to your .graphql query:

      shop {
        localTime {
          date
        }
      }
    

    Here is a link to the documentation:

    https://shopify.dev/docs/api/functions/reference/cart-checkout-validation/graphql/common-objects/shop