- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2024 09:36 AM
Hi all, I'm creating an RPA process and for part of it, we need to get the first and last day of the previous month. For example, if the process ran today, it would need to return 5/1/2024 and 5/31/2024. I can easily get the current date and subtract one month, but I'm having trouble getting it to return the first and last day of the month.
RPA has script actions and the ability to transform input or output, but I'm not getting these to work how I'd like. Do you have any ideas on this? Of course, this would need to function correctly going from January to December and leap years.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2024 11:07 AM
Hi @DylanB ,
Based on your requirement I've tried to create a logic through script. You can use this in BR, Script include, or in the script action. I've pasted the code below, Try it and see if that works.
And feel free to customize the script according to your requirement.
function getFirstAndLastDayOfPreviousMonth() {
// Get the current date
var today = new GlideDateTime();
// Set the date to the first day of the current month
today.setDayOfMonth(1);
// Subtract one day to get the last day of the previous month
today.addDaysLocalTime(-1);
// Copy the current date to get the last day of the previous month
var lastDayPreviousMonth = new GlideDateTime(today);
// Set the date to the first day of the previous month
var firstDayPreviousMonth = new GlideDateTime(today);
firstDayPreviousMonth.setDayOfMonth(1);
// Format the dates as needed (YYYY-MM-DD)
var firstDayFormatted = firstDayPreviousMonth.getLocalDate().toString();
var lastDayFormatted = lastDayPreviousMonth.getLocalDate().toString();
return {
firstDay: firstDayFormatted,
lastDay: lastDayFormatted
};
}
// You can use the function something like this.
var dates = getFirstAndLastDayOfPreviousMonth();
gs.info("First Day of Previous Month: " + dates.firstDay);
gs.info("Last Day of Previous Month: " + dates.lastDay);
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2024 11:07 AM
Hi @DylanB ,
Based on your requirement I've tried to create a logic through script. You can use this in BR, Script include, or in the script action. I've pasted the code below, Try it and see if that works.
And feel free to customize the script according to your requirement.
function getFirstAndLastDayOfPreviousMonth() {
// Get the current date
var today = new GlideDateTime();
// Set the date to the first day of the current month
today.setDayOfMonth(1);
// Subtract one day to get the last day of the previous month
today.addDaysLocalTime(-1);
// Copy the current date to get the last day of the previous month
var lastDayPreviousMonth = new GlideDateTime(today);
// Set the date to the first day of the previous month
var firstDayPreviousMonth = new GlideDateTime(today);
firstDayPreviousMonth.setDayOfMonth(1);
// Format the dates as needed (YYYY-MM-DD)
var firstDayFormatted = firstDayPreviousMonth.getLocalDate().toString();
var lastDayFormatted = lastDayPreviousMonth.getLocalDate().toString();
return {
firstDay: firstDayFormatted,
lastDay: lastDayFormatted
};
}
// You can use the function something like this.
var dates = getFirstAndLastDayOfPreviousMonth();
gs.info("First Day of Previous Month: " + dates.firstDay);
gs.info("Last Day of Previous Month: " + dates.lastDay);
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-24-2024 07:47 AM
Hi there, thanks for the reply! I've played with that script and landed on using this one.
var lastMonth = new GlideDate();
lastMonth.addMonths(-1);
lastMonth.setDayOfMonth(35);
var formattedDate = lastMonth.getByFormat("MM-dd-yyyy");
return formattedDate;
I'm using this script in a subflow. The subflow has a flow variable that runs the above script and outputs the date as the subflow output.