set date/time variable to 1 of 2 dates based on current date/time

booher04
Tera Guru

Within a catalog item, I have a need to set a planned_start_date(date/time variable) to either the 8th or the 20th of the current month based on the current date/time.  For example:  if today is 1/12/2023, I need the planned_start_date to be set to 1/20/2023.  If the current date was 1/22/2023 I'd need the planned_start_date to be 2/8/2023.  We will only be setting it to the 8th or the 20th of the month.  I'd like this to be within the workflow in a RUN Script activity if possible, but a client script is also an option. Below is what I tried but it's not working.  Any ideas?

 

var gdt = new GlideDateTime();
var dayOfMonth = gdt.getDayOfMonthLocalTime();
if( dayOfMonth > 8 ) {
	current.variables.planned_start_date = dayOfMonth(20);
}
else {
	current.variables.planned_start_date = dayOfMonth(8);
}

 

2 ACCEPTED SOLUTIONS

AndersBGS
Tera Patron
Tera Patron

Hi @booher04 ,

 

Below should work:

(function executeRule(current, previous /*null when async*/) {

var gdt = new GlideDate;
var date = gdt.getByFormat("yyyy-MM-dd");
var dateInMonth = gdt.getDayOfMonthUTC();
var findDaysDifference = 0;
var futureDate = 0;
if(dateInMonth > 8 && dateInMonth <= 19) {
	findDaysDifference = 20 - dateInMonth;
	gdt.addDays(findDaysDifference);
} else if(dateInMonth <= 20 && dateInMonth >= 31) {
	findDaysDifference = dateInMonth - 8;
	gdt.addMonthsLocalTime(1);
	gdt.addDays(findDaysDifference);
} else {
	findDaysDifference = 8 - dateInMonth;
	gdt.addDays(findDaysDifference);
}
futureDate = gdt.getDisplayValue();

gs.info("Todays date is "+ date)
gs.info("Todays date number is "+ dateInMonth);
gs.info("Count until future date is "+ findDaysDifference);
gs.info("Set future date to "+ futureDate);

})(current, previous);

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

View solution in original post

I just realize that planned_start_date looks to actually be a Date/time (not a Date), so the correct code is

current.variables.planned_start_date = setForThe8th(nowGDT, dayOfMonthNow) || 
	setForThe20th(nowGDT, dayOfMonthNow) || 
	setForNextMonth(nowGDT);

not

current.variables.planned_start_date = (setForThe8th(nowGDT, dayOfMonthNow) || 
	setForThe20th(nowGDT, dayOfMonthNow) || 
	setForNextMonth(nowGDT)).getDate();

There is no need to convert the resultant GlideDateTime into a GlideDate.

View solution in original post

19 REPLIES 19

AndersBGS
Tera Patron
Tera Patron

Hi @booher04 ,

 

Below should work:

(function executeRule(current, previous /*null when async*/) {

var gdt = new GlideDate;
var date = gdt.getByFormat("yyyy-MM-dd");
var dateInMonth = gdt.getDayOfMonthUTC();
var findDaysDifference = 0;
var futureDate = 0;
if(dateInMonth > 8 && dateInMonth <= 19) {
	findDaysDifference = 20 - dateInMonth;
	gdt.addDays(findDaysDifference);
} else if(dateInMonth <= 20 && dateInMonth >= 31) {
	findDaysDifference = dateInMonth - 8;
	gdt.addMonthsLocalTime(1);
	gdt.addDays(findDaysDifference);
} else {
	findDaysDifference = 8 - dateInMonth;
	gdt.addDays(findDaysDifference);
}
futureDate = gdt.getDisplayValue();

gs.info("Todays date is "+ date)
gs.info("Todays date number is "+ dateInMonth);
gs.info("Count until future date is "+ findDaysDifference);
gs.info("Set future date to "+ futureDate);

})(current, previous);

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Thanks for the response!  I do not see where the variable(planned_start_date) is being set in that script.  Am I missing it?

Hi @booher04 ,

 

variable planned_start_date = futureDate

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Anders, yes I did that thank you.  It's almost working, however instead of it being the current time it's 5 hours behind.  1/13/2023 at 11:00am I entered... the date that populated was 1/20/2023 at 6:00:am.  The variable is a date/time variable so I added in the T00:00:00 to the getByFormat() line.  

 

(function executeRule(current, previous /*null when async*/) {

var gdt = new GlideDateTime;
var date = gdt.getByFormat("yyyy-MM-ddT00:00:00");
var dateInMonth = gdt.getDayOfMonthUTC();
var findDaysDifference = 0;
var futureDate = 0;
if(dateInMonth > 8 && dateInMonth <= 19) {
	findDaysDifference = 20 - dateInMonth;
	gdt.addDays(findDaysDifference);
} else if(dateInMonth <= 20 && dateInMonth >= 31) {
	findDaysDifference = dateInMonth - 8;
	gdt.addMonthsLocalTime(1);
	gdt.addDays(findDaysDifference);
} else {
	findDaysDifference = 8 - dateInMonth;
	gdt.addDays(findDaysDifference);
}
futureDate = gdt.getDisplayValue();
current.variables.planned_start_date = futureDate;

gs.info("Todays date is "+ date)
gs.info("Todays date number is "+ dateInMonth);
gs.info("Count until future date is "+ findDaysDifference);
gs.info("Set future date to "+ futureDate);

})(current, previous);