Calculate number of days in the month and add 1


I am looking for the bot to be able to calculate the number of days in the month and then add/subtract 1 to that.
E.g. april has 30 days so i need the but to be able to enter 30/04/2020 +/- 1

I have a code that can give me the end of the month but every month is different

can anyone help me?

your question is not clear @nick.v can you pls elaborate.

this line is not clear … not getting what exactly you are trying to explain

If you have the calculation for the end of the month stored in a variable (e.g. MyDatetime), and the variable is a Datetime datatype, you can use MyDatetime.AddDays(1) to get the day after, and MyDatetime.AddDays(-1) to get the day before.

If your date is a string formatted as dd/MM/yyyy, you’ll need to convert it to a datetime first. If MyStr is your date as a String datatype, you can set MyDatetime to Datetime.ParseExact(MyStr, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture), and then you can use the AddDays method on MyDatetime.

I am not sure what you want but this might help you. And maybe someone has a prettier solution :slight_smile:

End of this month:
yourDate = New datetime(now.Date.Year, now.Date.Month,1).AddMonths(1).AddDays(-1)

End of previous month:
New datetime(now.Date.Year, now.Date.Month,1)AddDays(-1)

Great thanks that has worked, however when i am running the bot it wont always be 1 day that needs to be added or deleted e.g. some pdf will have 9 days that need to added some will have 4 days to be subtracted.
How can i add this to the code? @Anthony_Humphries

You’ll need to create a variable of type Int32 to store the days. You can put this variable in place of the hard-coded 1 or -1. You’ll need to use an activity to get this number from wherever you’re storing the days.

brilliant yes i am using ocr to store the data then ill enter it into the code. thank you

so if i was to replace the +1 in the assign code to the int32 variable the code should work?

MyDateTime = Now.AddDays(Date.DaysInMonth(Now.Year, Now.Month) - Now.Day +1)

Type into
MyDatetime.ToString (“MMddyyyy”)


Yes, and you will need to use + MyVar, where MyVar is your variable.

brilliant thank you @Anthony_Humphries

i seem to be getting this error when i run the process


When i change the variable to Int32 i get a different error as follows below

It looks like you’re assigning a string to your variable. The value of the variable must be set to an integer, and the datatype of the variable must be Int32.

its a string as i need the format to be in MM/dd/yyyy format.


See my first post in this thread for how to convert a String to a Datetime.

ok so would i put that in the = in assign or in the variable default?

I recommend using Assign to make sure your other variables are already instantiated.

GGrrr this is so annoyingly, i’ve got errors and am not sure why :neutral_face:
Last day of the month.xaml (6.7 KB)

please can you direct me, where am i going wrong


See attached:

Last day of the month.xaml (6,8 Ko)

ive just ran that work flow and i got this error


What’s the value of MyVar after the first Get Text activity is run (seen in the locals panel)?