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

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.

This is what actually worked for me.  The only issue I am running into is setting the time to 9:00PM instead of the current time.  

The question is 9:00PM in which time zone? That of the system (UTC), or of a location/office, or that of the current user (which may be different, even temporarily, than that of the office the user is in)?

I think I'm responding to you here and on another thread so I apologize.  

Well that is kind of tricky as most are in EST but we do have a couple that are in CST as well.  I can set it to EST and then work around with that probably. 

I believe it is. If you want to enforce EST,

- the output of those functions should be cached in a variable

- the TZ or the resulting GlideDateTime should be force-set EST

- the local time should be set to the desired time

- than it should be used to set the variable.

Will cook up a solution in a minute.