How to specify to To Date to always be the last day of the prior month

I am creating an automation to pull data from SAP. In the SAP selection screen I have validity dates - from date and to date. I created two variables - fromDt and toDt. I defined the fromDT with a default of


and it will generate the 1st day of the prior month.

What default value goes in the toDt variable - so it will return 03/31/2017 if the current date is 04/24/2017…but that it would also return 04/30/2017 when the current date is in May?

now.AddDays(1 - now.Day).AddMonths(-1).ToShortDateString - 1st day of Last Month

now.Date.AddDays(-now.Day).ToShortDateString - Last day of  Last Month

Is this what you neeD?


Hello - thanks!

This is working great right now - it correctly pulls in March 1 and March 31! Just to confirm…this works for all months, regardless of the current date?


It should, try changing Your machine date to next month to test

I want 2nd working day of the month. if it is saturday then the 2nd working day would be monday same if it is sunday then last workig day would be monday.


one way to achieve given months first working day:
Get first day of the month → myDate = new DateTime(2017, myMonth, 1)
While myDate.DayOfWeek is saturday or sunday-> Add one day

Here’s an example
Main.xaml (6.2 KB)


1 Like

Thanks Topi - this is great but we will have to finagle with this a bit as we now have a report that is run on 5th workday and both Saturday, Sunday, and major US holidays are not considered workdays. For example, if Friday is July 1, then Monday is July 4. In this case, Workday 1 is Friday, July 1. Workday 2 is Tuesday, July 5, and Workday 5 would then be Friday July 8.

I know we can handle ‘add one day for Sunday and add two days for Saturday’ but are there any capabilities to work around major US holidays? There are only 6 or 7 that we follow so we could store the physical dates in a variable, although then we’d have to update them every year…I’m looking for something a bit more elegant.


Might help someone to build their reusable workflow.

NewYear = DateTime(date.Year, 1, 1)).DayOfYear;
ChristmasEve= DateTime(date.Year, 12, 24)).DayOfYear;
Christmas= DateTime(date.Year, 12, 25)).DayOfYear;
July4th = DateTime(date.Year, 7, 4)).DayOfYear;

First Monday in September
DateTime LaborDay = new DateTime(date.Year, 9, 1);
DayOfWeek dayOfWeek = laborDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
laborDay = laborDay.AddDays(1);
dayOfWeek = laborDay.DayOfWeek;
date.DayOfYear == laborDay.DayOfYear;

//Last Monday in May
DateTime memorialDay = new DateTime(date.Year, 5, 31);
DayOfWeek dayOfWeek = memorialDay.DayOfWeek;
while (dayOfWeek != DayOfWeek.Monday)
memorialDay = memorialDay.AddDays(-1);
dayOfWeek = memorialDay.DayOfWeek;
date.DayOfYear == memorialDay.DayOfYear;

//4th Thursday in November
var thanksgiving = (from day in Enumerable.Range(1, 30)
where new DateTime(date.Year, 11, day).DayOfWeek == DayOfWeek.Thursday
select day).ElementAt(3);
DateTime thanksgivingDay = new DateTime(date.Year, 11, thanksgiving);
date.DayOfYear == thanksgivingDay.DayOfYear;


Thank you @vvaidya Vinay!

Hi Topi @Topi

Thanks - we tested this and it works great for finding the first working day. We thought that we could change the first assign to be (month, 4) if we want the 4th working day but it doesn’t work. What else do we need to change to get this to function to give us the 4th, 5th, or 6th workday of a month? Consider the month of July, 2017…even throwing the July 4th holiday in the US out of scope, the first workday in July 3, the 2nd is July 4, the 3rd is July 5, the 4th is July 6, the 5th is July 7, and the 6th is July 10.

Your .xaml works great to get the first workday. But if we change to 6 then it doesn’t work. What else needs to change?



not sure what your desired output is, but here’s a simple loop that will check all the working days in July.
Main.xaml (8.0 KB) // Minor fix required, change the counter condition to <=


Thanks, Topi! this is working great as you sent it. I’m now trying to introduce - for the month of July, for example, the fact that July 4th is indeed a holiday and thus a non working day. I added on to your Condition.

Your original condition is this:
new DateTime(2017, 7, counter).DayOfWeek = DayOfWeek.Saturday or new DateTime(2017, 7, counter).DayOfWeek = DayOfWeek.Sunday

I added on (shown in bold) so it looks like this, but it’s barking at me:
new DateTime(2017, 7, counter).DayOfWeek = DayOfWeek.Saturday or new DateTime(2017, 7, counter).DayOfWeek = DayOfWeek.Sunday or new DateTime(2017, 7, counter).Date = DateTime(date.Year, 7, 4).DayOfYear

I tried a couple of other ways to say ‘July 4’ but it kept barking. Can you assist one more time? I’m thinking if I include July 4, January 1, December 25 explicity that will take care of all but three holidays and only 1 of them - Labor Day, would we have to handle manually as it is the only ‘floating’ day that is early enough in the month to potentially cause a shift in the 1st - 6th work days.


Have you tried outputting what new DateTime(2017, 7, counter).Date and DateTime(date.Year, 7, 4).DayOfYear are equal to which can help you figure out the correct syntax. I’m thinking you need “new” infront of DateTime.

new DateTime(2017, 7, counter).Date = new DateTime(date.Year, 7, 4).DayOfYear

It might also be beneficial to include the holidays in an array variable that can be retrieved also from a file. If you have say {“July 4”,“December 25”} for example, then you can use Array.IndexOf() to determine if the date is a holiday.

Array.IndexOf(arrHolidays, new DateTime(2017, 7, counter).Date.ToString(“MMMM d”))

Also keep in mind that the year will change so you might need to use Year(Now) instead of 2017

Hope this helps some, but I have not tested any of this :wink:



1 Like


simplest way here to outrule 4th of July is to just check if the counter is 4. So basically or counter = 4.
@ClaytonM is correct in the ‘new’ missing, but also ‘date’ which you are using does not have Year property if i remember correctly. In future please include what UiPath is actually telling as the reason of the error.


Hi Vaidya - We are trying to use your example to determine Labor Day and we put it into an Invoke Code activity. However it is barking at us. Here is the code and the arguments. Can you see what we are missing?LaborDay.pdf (165.8 KB)

How to get last date of current month ?


Hello Hemanth,

I’ve been poking at this and this works for me.

This will return the first day of the current month:
now.AddDays(1 - now.Day).ToShortDateString

This will return the last day of the current month:
now.AddDays(1 - now.Day).AddMonths(1).AddDays(-1).ToShortDateString

There may be a more elegant solution but this works regardless of the months involved.


1 Like

thank you @mgirishfan, it really works well. :slight_smile:
I have assigned a value using assign task and sent it as end date(something like in .Net)
end date = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month)).ToString(“MM/dd/yyyy”)


@vvaidya Would you know if there is a way to find current Months last date? Thanks in Advance

mentioned above


new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year,now.Month)).ToShortDateString