JS showing NaN for a date calculation - unclear why it says the date format is Invalid

I’m attempting to display a calculation between a recorded date and now. Here is the format I am using, which is a timestamp format using “F jS, Y g:i A”

Screen Shot 2024-03-14 at 10.49.18 PM

The JS is as follows which I was able to add a line (after getting a NaN) to check for date format error. And for some reason, it’s claiming the format is the issue.

Does it expect a specific format?

// Access the application_date from the global variable
const applicationDate = collections['834er0nwe0nw0bwgw']?.['data']?.['application']?.['application_date'];

// Parse the application_date to a JavaScript Date object
const parsedApplicationDate = new Date(applicationDate);

// Check if parsedApplicationDate is a valid date
if (isNaN(parsedApplicationDate.getTime())) {
  return "Invalid date"; // Or handle the error appropriately
}

// Get the current date and time
const now = new Date();

// Calculate the difference in milliseconds
const diffInMilliseconds = now.getTime() - parsedApplicationDate.getTime();

// Convert the difference from milliseconds to days
const diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);

// Round the number of days to the nearest whole number
const roundedDiffInDays = Math.round(diffInDays);

// Return the number of days
return roundedDiffInDays;

a Date object already returns “Invalid date” with the toString method if it was constructed with an invalid date, so you can directly check for that instead of using isNan.

Are you sure you are accessing the applocation date the right way? Usually collection’s data is an array, not an object

If I knew enough to answer your question, I would :smile:

Does this help? It’s a timestamp in the database.

Screen Shot 2024-03-16 at 2.57.14 PM