Stock market hours API with JAVASCRIPT.

Stock market hours API with JAVASCRIPT.

by Financial Modeling Prep

HTML

<div class="container">
  <div class="stock-name"></div>
  <div class="time"></div>
</div>

CSS

body {
  font-family: Helvetica Neue, Arial, sans-serif;
  font-size: 14px;
  color: #444;
}

.time {
  padding: 10px;
}

.table {
  border: solid 1px #DDEEEE;
  border-collapse: collapse;
  border-spacing: 0;
  font: normal 13px Arial, sans-serif;
}

.table thead th {
  background-color: #f1efb8;
  border: solid 1px #DDEEEE;
  color: #336B6B;
  padding: 10px;
  text-align: center;
  text-shadow: 1px 1px 1px #fff;
}

.table tbody td {
  border: solid 1px #DDEEEE;
  color: #333;
  padding: 10px;
  text-shadow: 1px 1px 1px #fff;
  min-width: 80px;
  text-align: center;
}

.stock-name {
  padding: 10px;
}

.container {
  height: 100px;
}

JavaScript

document.addEventListener('DOMContentLoaded', () => {
   // https://financialmodelingprep.com/developer/docs
   getRequest(
     'https://financialmodelingprep.com/api/v3/is-the-market-open',
     drawOutput
   );

   function drawOutput(responseText) {

     let resp = JSON.parse(responseText);

     console.log(resp);

     var elements = document.querySelectorAll('.stock-name')[0];
     
     var isOpen = 'no';
     
     if(resp.isTheStockMarketOpen)
     		isOpen = 'yes';
     
     elements.innerHTML += 'Is the market open today: ' + isOpen;

     var time = document.querySelectorAll('.time')[0];

   }

   function getRequest(url, success) {
     var req = false;
     try {
       req = new XMLHttpRequest();
     } catch (e) {
       try {
         req = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
         try {
           req = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {
           return false;
         }
       }
     }
     if (!req) return false;
     if (typeof success != 'function') success = function() {};
     req.onreadystatechange = function() {
       if (req.readyState == 4) {
         if (req.status === 200) {
           success(req.responseText)
         }
       }
     }
     req.open("GET", url, true);
     req.send(null);
     return req;
   }
 })