UNPKG

stonkinator

Version:

A low level stock data aggregation tool, a boring lib for others to build upon

54 lines (53 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NewsDateParser = void 0; /** A Date Parser to convert news dates to JS Date Objects * @internal * @param Date - A Date string in the format `MONTH, DATE, YEAR at TIME (a.m. or p.m.)` Must be in eastern time. * @returns A javascript date object with the parsed date * @example * ``` * NewsParser("Oct. 13, 2020 at 11:55 a.m.") * ``` */ const NewsDateParser = (date) => { try { const abbreviations = { "Jan.": 0, "Feb.": 1, "Mar.": 2, "Apr.": 3, "May.": 4, "Jun.": 5, "Jul.": 6, "Aug.": 7, "Sep.": 8, "Oct.": 9, "Nov.": 10, "Dec.": 11, }; const split = date.split(" "); if (!Object.keys(abbreviations).includes(split[0])) throw "Bad Month"; const monthIndex = abbreviations[split[0]]; const day = parseInt(split[1].replace(",", ""), 10); const year = parseInt(split[2], 10); const type = split[5]; // eslint-disable-next-line prefer-const let [hours, minutes] = split[4].split(":").map(x => parseInt(x, 10)); //AM/PM parser if (type == "p.m." && hours < 12) hours = hours + 12; if (type == "a.m." && hours == 12) hours = hours - 12; //creating date const UTCDate = new Date(Date.UTC(year, monthIndex, day, hours, minutes, 0, 0)); // //Fixing Timezone Discrepancy UTCDate.setTime(UTCDate.getTime() + 300 * 60 * 1000); return UTCDate; } catch (err) { throw "Error while parsing date"; } }; exports.NewsDateParser = NewsDateParser;