- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2023 06:53 AM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2023 10:42 PM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 09:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 08:10 AM
Hi @booher04 ,
So after adding T00:00:00 to the getByFormat() line, it’s working as expected? Or de we need to modify?
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 08:12 AM
It's showing 5 hours behind my current time for some reason. so it's 11:12:00am right now, the date shows as 6:12:00am on the planned_start_date when it's populated. Is there a way to add 5 hours to the time? That may resolve the issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 08:59 AM - edited 01-13-2023 09:00 AM
That time offset is because the code is not correct:
futureDate = gdt.getDisplayValue();
current.variables.planned_start_date = futureDate;
is the same as:
current.variables.planned_start_date = gdt.getDisplayValue();
in other words a date expressed in the TZ of the current user (gdt.getDisplayValue()) is attributed to a property (planned_start_date) that expects a date expressed in UTC.
It should be either
current.variables.planned_start_date = gdt.getValue();
or (not sure if Date/time GlideVariableElement supports this method - si this might not work actually):
current.variables.planned_start_date.setDisplayValue(gdt.getDisplayValue());
But I would write as solution something like this:
var nowGDT = new GlideDateTime(),
dayOfMonthNow = nowGDT.getDayOfMonthUTC();
current.variables.planned_start_date = (setForThe8th(nowGDT, dayOfMonthNow) ||
setForThe20th(nowGDT, dayOfMonthNow) ||
setForNextMonth(nowGDT)).getDate();
function setForThe8th (nowGDT, dayOfMonthNow) {
if (dayOfMonthNow < {
nowGDT.setDayOfMonthUTC(8 );
return nowGDT;
}
}
function setForThe20th (nowGDT, dayOfMonthNow) {
if (dayOfMonthNow < 20) {
nowGDT.setDayOfMonthUTC(20);
return nowGDT;
}
}
function setForNextMonth (nowGDT) {
nowGDT.setMonthUTC(nowGDT.getMonthUTC() + 1);
nowGDT.setDayOfMonthUTC(8 );
return nowGDT;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 09:14 AM
Thank you for the response. Your solution didn't work at all. I did have to add in "if (dayOfMonthNow < 😎 " as the 😎 was missing. However, even after adding that it set the field to 01-19-2023 07:00:00 PM, instead of 01-20-2023 12:12:00 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 09:23 AM
Yeah, unfortunately the site turned 8 ) into a smiley face.
Also the hour reset (12:12:00 -> 07:00:00) is because the original code assumed a date is needed so it stripped away the hour part. A follow up post corrected that.