as part of my API call I am also embedding date information in URL
var url = “http://some_external.site.com&start_date=1453161660”;
var result = HTTP.call(‘GET’, url, {}, function (err, resp) {
}
while this corresponds to a fixed time information, (Tue Jan 19 00:01:00 2016), how to limit time stamp to within the last 24 hours so while my app is running, everyday it is only pulling down the last 24 hours information? Or is that the responsibility for the people generating the API?
Cheers
Are you not simply looking for:
const now = new Date().getTime();
const yesterday = now - (24 * 60 * 60 * 1000);
const endpoint = `http://some_external.site.com&start_date=${yesterday}`;
HTTP.call('GET', endpoint, {}, (err, res) => {
// do work
});
Hi Stephen
It looks so simple once I see it, thanks a lot.
EDIT: looking for next 24 hours to be precise. So I guess it would look like
const now = new Date().getTime();
const tomorrow = now + (24 * 60 * 60 * 1000);
const endpoint = http://some_external.site.com&start_date=${tomorrow }
;
HTTP.call(‘GET’, endpoint, {}, (err, res) => {
// do work
});
Seb
But why defined these as const when these will updated on daily basis?
If you are indeed trying to predict the future, than yes; add the time difference instead of subtracting it. The const
instead of var
or let
is simple. It is a constant
within the scope of the function. So every time you run that script, the const
can be assigned a new value (be declared with a different value to be exact). But the const
cannot be assigned a new value one line later.
So this example is not giving any problems:
const add = (a, b) => {
const c = a + b;
return c;
}
add(1, 2);
add(2, 3);
But this one does, because I’m reasigning a new value to c
. That’s not allowed.
const add = (a, b) => {
const c = a + b;
c = c + 10;
return c;
}
add(1, 2);
There is a difference in the quotes you are using.
const url = "http://mysite.com:8000/service=206&assignment_group=Enterprise%20Network%20Development&start_date=1453161660";
These double quotes are representing a string. You are telling the compiler to parse as is. And so do the single quotes in the quoted code below.
const url = 'http://mysite.com:8000/service=206&assignment_group=Enterprise%20Network%20Development&start_date=${tomorrow}';
In the last one that “doesn’t parse”, you seem to have forgotten a =
sign to separate a key from a value.
If you simply change to one of these to examples, It should work. But there is a change that the spaces in your url (%20
) will give you some trouble.
const timestamp = (new Date().getTime()) + (24 * 60 * 60 * 1000);
const url = 'http://mysite.com:8000/service=206&assignment_group=Enterprise%20Network%20Development&start_date=' + timestamp;
const timestamp = (new Date().getTime()) + (24 * 60 * 60 * 1000);
const url = `http://mysite.com:8000/service=206&assignment_group=Enterprise%20Network%20Development&start_date=${timestamp}`;
Note that the last example uses backticks instead of single / double quotes.