I've tried multiple different ways, but I'm lost. I can't seem to remove the "Invalid Date" that gets outputted. The Invalid Date occurs because of a blank entry from the JSON. Alas, I have given a workable example without using AJAX which produces the same result.
$(document).ready(function() {
var datePassed = "20170715";
var inval = "";
var newDateString = datePassed.substr(4,2) + "-" + datePassed.substr(6,2) + "-" + datePassed.substr(0, 4);
var finalDate = new Date(newDateString.replace(/(?:Invalid Date)/g, " "));
var fakeDate = new Date(inval.replace(/(?:Invalid Date)/g, " "));
console.log(finalDate);
console.log(fakeDate);
});
Invalid Date gets appended to my HTML and it's not cool.
Edit: Some people seem to be under the impression all I want to do is validate whether or not I get an invalid date back. I expect them to come; it's simply part of how I arrange things. I want "Invalid Date" removed from my HTML as it is appended to my class like so:
this.elem.find(".deadline").append(finalDate);
Some data comes through null and some comes with an actual date. That's just how the JSON data is. I would like answers to directly address how to replace a string with regex and not use any other method.