stonkinator
Version:
A low level stock data aggregation tool, a boring lib for others to build upon
58 lines (57 loc) • 1.93 kB
JavaScript
;
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.")
* ```
*/
var NewsDateParser = function (date) {
try {
var 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,
};
var split = date.split(" ");
var monthIndex = abbreviations[split[0]];
var day = parseInt(split[1].replace(",", ""), 10);
var year = parseInt(split[2], 10);
var type = split[5];
// eslint-disable-next-line prefer-const
var _a = split[4].split(":").map(function (x) { return parseInt(x, 10); }), hours = _a[0], minutes = _a[1];
//AM/PM parser
if (type == "p.m." && hours < 12)
hours = hours + 12;
if (type == "a.m." && hours == 12)
hours = hours - 12;
//creating date
var UTCDate = new Date();
UTCDate.setUTCDate(day);
UTCDate.setUTCMonth(monthIndex);
UTCDate.setUTCFullYear(year);
UTCDate.setUTCMinutes(minutes);
UTCDate.setUTCHours(hours);
UTCDate.setUTCSeconds(0);
// //Fixing Timezone Discrepancy
UTCDate.setTime(UTCDate.getTime() + 300 * 60 * 1000);
return UTCDate;
}
catch (err) {
throw "Error while parsing date";
}
};
exports.NewsDateParser = NewsDateParser;