JSFiddle - React, Tailwind, and code Playground

by frogggeee McFrogggeee

JavaScript

function toDay(time) {
  const timestamp = time;
  const date = new Date(timestamp);
  return (date.toString());
}
function formatDate(timestamp) {
  let date = new Date(timestamp);
  return date.toISOString().split('T')[0]; // get the date part in "YYYY-MM-DD" format
}
let time = {
  year: 2023,
  month: 5,
  day: 1,
  days: 25
  // limit: 3 days at a time
}

let startDate = new Date(Date.UTC(time.year, time.month, time.day))
let timestamp = (startDate.getTime() - (1000 * 60 * 60 * 0)); // add 5 hours to first timestamp
let endDate = new Date(Date.UTC(time.year, time.month, time.day+(time.days*0)));
let timestamp2 = (endDate.getTime() + (1000 * 60 * 60 * 24 * (time.days )));

async function fetchStockData() {
  const url = "https://api.polygon.io/v2/aggs/ticker/" + "TSLA" + "/range/1/" + "minute" + "/" + timestamp + "/" + timestamp2 + "?adjusted=true&sort=asc&limit=50000&apiKey=NLuCX1X16ryXihd1oQCMIOe2D_1Q6mAp";

  try {
    const response = await fetch(url);

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    let data = await response.json();
    console.log(data.results.length + " results")
    console.log(time.days + " days")
    console.log(toDay(data.results[0].t))
    console.log(toDay(data.results[data.results.length-1].t))
        console.log(toDay(timestamp2))
  } catch (error) {
    console.log("Hey, you have a error fetching data:", error);
    
    
  }
}
fetchStockData()