New to JavaScript, Help Please

I Am trying to create a script that appends a new line to a draft with various information for recording expenses. I am getting hung up on the first item the date. Below I have tried to combine a couple of examples to select and format a date.

The error I get is:

Script Error: TypeError: undefined is not an object (evaluating ‘now.getFullYear’)

Line number: 6, Column 17

I am thinking that it has to do with my passing myDate to the function. Can anyone point me in the right direction?

function getDateTime(now) {
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
if(month.toString().length == 1) {
var month = ‘0’+month;
}
if(day.toString().length == 1) {
var day = ‘0’+day;
}
var dateTime = year+’-’+month+’-’+day;
return dateTime;
}

var p = Prompt.create();
p.addDatePicker(“myDate”, “Date Field”, new Date(), {
“mode”: “date”});

// p.addSelect(“myCategory”, “Expense Category”, [“Airfare”, “Lodging”, "Meals "], [], false);

p.addButton(“Ok”);

if (p.show()) {
editor.setSelectedText(getDateTime(p.myDate));
var selRange = editor.getSelectedRange();
editor.setSelectedRange(selRange[0]+selRange[1],0);
};

You were not getting the myDate field from the prompt, but instead attempting to get the myDate property from the prompt object.

Try something like this:

function getDateTime(p_now)
{
	let year = p_now.getFullYear();
	let month = p_now.getMonth() + 1;
	let day = p_now.getDate();
	if (month.toString().length == 1)
	{
	month = '0' + month;
	}
	if (day.toString().length == 1)
	{
		day = '0' + day;
	}
	return year + '-' + month + '-' + day;
}
	
let p = Prompt.create();
p.addDatePicker("myDate", "Start date", new Date(), {
  "mode": "date"
});
p.addButton("Ok");


if (p.show())
{
	editor.setSelectedText(getDateTime(p.fieldValues["myDate"]));
	let selRange = editor.getSelectedRange();
	editor.setSelectedRange(selRange[0] + selRange[1], 0);
};

I’ve made a few other tweaks as well, but look out for the fieldValues use. Hope it helps

I’d also suggest posting this as a code listing (like above), or as a link to an action in the future. I had to spend a while just reformatting and swapping out smart quotes before I could get a proper view of what was going on.