KMJO
KMJO

Reputation: 95

How to handle date format for a possibly empty cell?

From a Google Form I have created, 50% of users are entering a date as an answer to a question, the other 50% will not be entering anything so the cell contents in the response form is empty. I have written some code that puts the contents of the cell into a certain format.

I'm exporting the data to a Google Doc, at the moment if the date is not entered, "Thu 01 Jan 1970" appears as an output. I would like this to be "N/A" instead.

I've left out most of the code as don't think it is of any use:

var enddate = vS.getRange(vLastRow,12);
var vEndDateValue = new Date (enddate.getValues());
var vEndDateFormat = Utilities.formatDate(vEndDateValue, 'BST', 'EEE dd MMM YYYY');

Upvotes: 1

Views: 1444

Answers (1)

Mogsdad
Mogsdad

Reputation: 45750

You simply need to test the value read from the spreadsheet first:

var enddate = vS.getRange(vLastRow,12);
var cellContent = enddate.getValue();
var vEndDateValue = new Date (cellContent);
var vEndDateFormat = cellContent ? 
                     Utilities.formatDate(vEndDateValue, 'BST', 'EEE dd MMM YYYY') :
                     "N/A";

Upvotes: 4

Related Questions