0

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.

2
  • what is a console.log(tableDate) before you make the Date object returning? Commented Nov 9, 2017 at 18:07
  • on the html page its just showing as "undefined" when i put console.log(tableDate) into the function that actually builds the table Commented Nov 9, 2017 at 18:13

1 Answer 1

1

I figured out what my problem was... I wasn't using the same format in my moment() function call as the format in the .csv file. Stupid mistake...oops.

But if anyone is interested, the above code works to compare times from csv file to current time (if you make sure you have the same format in both the csv file and the moment() function call.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.