How do you get the current time from bigcartel's server?

117 views Asked by At

Is there a way to get the current date and time from bigcartel's server, either with Liquid or JavaScript? I'm building some custom functionality in my store that needs to have a consistent (not client-side) time source. I would only need to access it once per page load.

Here's what I've tried so far, based on another date-related bigcartel question:

var currentDate = new Date({{ 'Today' | date: "%Y, %m, %d, %H, %M" }});
currentDate.setMonth(currentDate.getMonth() - 1);

This does retrieve today's date, but it doesn't consistently update as I refresh the page. Any help would really be appreciated. Thanks.

EDIT: Liquid will get a fresh date/time after each time I edit/save and reload a particular page, or if a certain amount of time has elapsed after the first page load (I haven't done enough testing to pin down exactly how much time... maybe 2 or 3 hours... or more?).

Also, the stuck date/time is unique to each product page. For example, if I load Product Page A at 9:30, and Product Page B at 9:32, they'll both be stuck on those respective times. Both pages are being rendered from the same code.

I've never worked with Liquid before, but I assume it should retrieve fresh data from the server with each page load, similar to PHP. Am I mistaken about that?

EDIT 2: It gets even weirder. Sometimes I have a query string appended to the end of the URL of a given product page (e.g. ?comicID=225). But if I change the number in the query string (or even just type in random garbage instead) it will retrieve a new stuck date. So it appears any unique URL gets a unique stuck date from whenever it first gets loaded!

1

There are 1 answers

0
Mike Elliott On

I ended up making an XMLHttpRequest() with JavaScript to get the current time from the HTTP response headers, and it seems to be working perfectly for my purposes. Here's my working code:

var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var currentDate = new Date(req.getResponseHeader('date'));