Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Client Script - getting date value

Rob Sestito
Mega Sage

Hey there SN Comm,

Looking for some help with a client script please.

Requirement:
Date field called 'Deadline' - When entered/changed, I need another field called 'Timing', to display certain choices:

If Deadline = today (timing = "overdue")

If Deadline =  in the future but less than 180 days from current system date, (timing = “Deadline Approaching”)

If Deadline = in the future but <= 12/31 of the current year, (timing = “This Year”)

If Deadline = in the future but > 12/31 of the current year, (timing = “Future”)

 

I tried creating a test client script so far to get me going, but I get Timing = "Undefined" in the field.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var theDeadline = g_form.getValue('u_deadline');
	var theTiming;
	var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
	var today = new Date();
	
	if (today == theDeadline) {
		theTiming = "Overdue";
	}
	
	g_form.setValue('u_timing', theTiming);
}

Anyone able to help?

John Feist - want to take another stab at more client scripts with me? LOL

Cheers!

-Rob

1 ACCEPTED SOLUTION

Rob Sestito
Mega Sage

I was able to figure out what I needed in order to make this work how I now want it too.

Adding my code in here in case it helps anyone. Making this as correct to close the post.

Thanks all!

-Rob

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	var theTiming;
	var today = new Date();
	
	//get the entered date string
	var dtstring = g_form.getValue('u_deadline');
		
	//convert entered date to Date object
	var dateToCheck = new Date(dtstring);
	
	var approaching = new Date();
	approaching.setDate(today.getDate() + 179);
	
	//Compare the two numbers
	if (dateToCheck < today || dateToCheck == today) {
		theTiming = "Overdue";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck <= approaching) {
		theTiming = "Within 6 Months";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck > approaching) {
		theTiming = "6+ Months";
		g_form.setValue('u_timing', theTiming);
	}
	
	else {
		theTiming = "Where did that Date come from?";
		g_form.setValue('u_timing, theTiming');
	}
}

View solution in original post

7 REPLIES 7

Hey vkachineni,

Thanks for replying - unfortunately, nothing at all happened with your code for me.

This is what I have thus far, that is working - however, when I choose a date from Deadline, to be anything in the year 2020, theTiming 'Timing' remains Deadline Approaching.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var theTiming;
	var today = new Date();
	
	//get the entered date string
	var dtstring = g_form.getValue('u_deadline');
	
	//convert the string to a JavaScript Date/Time
	var dt = new Date(dtstring);
	
	//get the current date/time
	var rightNow = new Date().valueOf();
	
	//convert entered date to milliseconds
	var dateToCheck = new Date(dtstring).valueOf();
	
	var approaching = new Date();
	approaching.setDate(today.getDate() - 180);
	
	var theFuture = new Date(new Date().getFullYear(), 11, 31);
	//theFuture.setDate(today.getDate());
	
	
	//Compare the two numbers
	if (dateToCheck < rightNow || dateToCheck == rightNow) {
		theTiming = "Overdue";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck >= approaching) {
		theTiming = "Deadline Approaching";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck > theFuture){
		theTiming = "Future";
		g_form.setValue('u_timing, theTiming');
	}
}

Thoughts?

-Rob

Rob Sestito
Mega Sage

Hey SN Comm,

If anyone is able to get me with this scripting - I would appreciate it! I have gotten far, but not far enough. I am still unable to figure out how to calculate for these two scenarios:

If Deadline (value form) is in the future but <= 12/31 of the current year, “This Year”

If Deadline (value form) is in the future but > 12/31 of the current year, “Future”

I Appreciate the help - here is my latest script (as I have been attempting many different ways) 

I have what I believe to be the two vars that I am sure are my issues.

When the deadline date field equals a future date more than 180 days from today, but before the new year, "this year" needs to appear in a separate read-only field (which I have working with Overdue and Deadline Approaching).

When the deadline date field equals a date into the new year, "future" will appear in the read-only field.

I definitely need some help scripting this part.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var theTiming;
	var today = new Date();
	
	//get the entered date string
	var dtstring = g_form.getValue('u_deadline');
	
	//convert the string to a JavaScript Date/Time
	var dt = new Date(dtstring);
	
	//get the current date/time
	var rightNow = new Date().valueOf();
	
	//convert entered date to milliseconds
	var dateToCheck = new Date(dtstring).valueOf();
	
	
	var approaching = new Date();
	approaching.setDate(today.getDate() - 180);
	
	/*var thisYear = new Date(new Date().getFullYear(), 11, 31);
	thisYear.setFullYear(2019);
	
	var theFuture = new Date(new Date().getFullYear(), 0, 01);
	theFuture.setFullYear(2020);
	*/
	
	
	
	//Compare the two numbers
	if (dateToCheck < rightNow || dateToCheck == rightNow) {
		theTiming = "Overdue";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck >= approaching) {
		theTiming = "Deadline Approaching";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateTocheck <= thisYear) {
		theTiming = "This Year";
		g_form.setValue('u_timing', theTiming);
	}
	
	else if (dateToCheck >= theFuture){
		theTiming = "Future";
		g_form.setValue('u_timing, theTiming');
	}
}

Cheers!

-Rob

Rob Sestito
Mega Sage

I was able to figure out what I needed in order to make this work how I now want it too.

Adding my code in here in case it helps anyone. Making this as correct to close the post.

Thanks all!

-Rob

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	var theTiming;
	var today = new Date();
	
	//get the entered date string
	var dtstring = g_form.getValue('u_deadline');
		
	//convert entered date to Date object
	var dateToCheck = new Date(dtstring);
	
	var approaching = new Date();
	approaching.setDate(today.getDate() + 179);
	
	//Compare the two numbers
	if (dateToCheck < today || dateToCheck == today) {
		theTiming = "Overdue";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck <= approaching) {
		theTiming = "Within 6 Months";
		g_form.setValue('u_timing', theTiming);
	}
	else if (dateToCheck > approaching) {
		theTiming = "6+ Months";
		g_form.setValue('u_timing', theTiming);
	}
	
	else {
		theTiming = "Where did that Date come from?";
		g_form.setValue('u_timing, theTiming');
	}
}