fawazahmed0 / exchange-api

Free Currency Exchange Rates API with 200+ Currencies & No Rate Limits
https://github.com/fawazahmed0/exchange-api#readme
Creative Commons Zero v1.0 Universal
729 stars 46 forks source link

Getting jsons with wrong dates #38

Closed Poccu closed 9 months ago

Poccu commented 2 years ago

Sometimes I get jsons with the wrong date on the "latest" request. Today is 2022-02-19

Example:

https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/cny.json

console.log on the project showing:

{
  "date": "2022-02-18",               <--- ❌ -1 day
  "cny": {
          "1inch": 0.094996,
          "ada": 0.154334,
          "aed": 0.579573,
          ...
          "zwl": 50.807804
          }
}

Also I tried to get xxx.min.json instead of xxx.json

If we open URL https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/rub.min.json everything is okay

date: "2022-02-19"               <--- ✔️
...

But when I console.log it on my project it shows

date: "2022-02-17"               <--- ❌ -2 days ??
...

Can you tell me what the problem is?

I'm trying to get the difference of exchange rates between the last two days - latest and (today - 1) day. So this is the problem for me.

fawazahmed0 commented 2 years ago

Your browser is caching the data.

You can do something like this: https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/cny.json?d=2022-02-19

or

Add a random number or string at end or url to fetch latest data https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/cny.json?rand=2343

I prefer appending date at end, because the rates are updated daily.

const UTCDateStr = new Date().toISOString().substring(0,10)
const avoidCache = '?d='+UTCDateStr
const url = 'https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/cny.json'
fetch(url+avoidCache).then(res=>res.json()).then(console.log);
Poccu commented 2 years ago

It works! Thanks a lot for the quick response :)

fawazahmed0 commented 9 months ago

currency-api has stopped working, Please read the migration guide