I am building a function that will read from a .csv file and build a table using ajax. My function works fine as far as building the table goes. One of the columns has a date/time in it. I am trying to convert the string version of the date into a date object so that I can compare the hour from the table to the current hour and set an error flag if the tableHour isn't within the last 2 currentHour.
I've tried alot of different suggestions from here and other sites but can't seem to get anything to work.
Any suggestions would be greatly appreciated.
if (cell_count === 9){
// cell_data[9] is the column from the .csv file that contains the date string
var tableDate = moment(cell_data[9], 'MM-DD-YYYY hh:mm:ss')
var date = new Date(tableDate);
var tableHour = date.getHours();
var currentDate = new Date();
var currentHour = currentDate.getHours();
// convert to local time
tableHour += 8;
if (tableHour > 23)
tableHour -= 24;
// 2 hour window
currentHour -= 2;
if (currentHour < 0)
currentHour += 24;
// flag to true if table < current
if (tableHour < currentHour)
error = true;
}
The remainder of my code works fine, and just builds the table and returns the table to an HTML document. Its just this function that is causing me issues.
As the code is now, all it returns is a table full of "NaN", which I'm assuming would mean that the date object I'm trying to create is invalid.
console.log(tableDate)before you make theDateobject returning?