cancel
Showing results for 
Search instead for 
Did you mean: 

Last day of the current month

NickNick
Level 3
A. AddDays(AddMonths(MakeDate(01; Mid(Today(); 4; 2);
Mid(Today(); 7; 4));1), -1)
B. MakeDate(01; Mid(Today(); 4; 2); Mid(Today(); 7; 4))
C. AddDays(MakeDate(01; Mid(Today(); 4; 2); Mid(Today(); 7; 4)), -1
D. None of the above

It seemed to me none of this are correct but answer says A. I tried all option on the system but all of them resulted in the error#
4 REPLIES 4

david.l.morris
Level 15
Where did this come from? None of those are good expressions because they do not force the date to be in a certain format before doing the string manipulation. So, depending on the regional settings, they could produce entirely different results. 

However, A does look to be the intended correct answer. I'll try to explain what I see. There are a couple of problems with trying out the expression. First, if your regional settings for date formats are different then it'll either error or give you a wrong result. Second, there are semicolons, so I don't know what's going on there. But, if you assume the semicolons would work or change them to commas, then the rest of it makes sense depending on regional settings.

A:
  • Mid(Today(); 4; 2) is intended to get the month, and Mid(Today(); 7; 4) is intended to get the year, both as numbers. It also appears to be expecting a date format of dd/MM/yyyy. 
  • If I resolve the two mid functions (assuming today is Feb 3rd, 2021) then the remaining expression looks like this:
    • AddDays(AddMonths(MakeDate(01; 2;2021);1), -1)
  • MakeDate(01; 2;2021) resolves to be a date of Feb 1st, 2021 (the goal was to get the first day of the current month.
  • Then AddMonths will add one month to the date so that we get March 1st, 2021.
  • Then AddDays (with -1) subtracts a day from that date so we get Feb 28th, 2021.


So, with that in mind, here's my expression to get the same thing, but 'should' work regardless of regional settings:
  • MakeDate(1, ToNumber(FormatDate(AddMonths(Today(),1),"MM")), ToNumber(FormatDate(AddMonths(Today(),1),"yyyy")))-MakeTimeSpan(1,0,0,0)

It could be less wordy with multiple expressions, but I wanted it in a single expression

Dave Morris, 3Ci at Southern Company

NickNick
Level 3
Thank you Dave Morris for the explanation. It came from a book called BluePrism MasterClass. 

My system setting shows YYYY-MM-DD at taskbar

Hi Nick

Can you give a bit of info on what it is exactly youre trying to do? is it literally just that you want the last day of the current month? if so then your function should look like this AddDays(AddMonths("01/"&FormatDate(Today(),"MM/yyyy"), 1),-1) This will get the first day of next month and minus it by one day giving the last day of this month but you can set the format to however you want the date to look it doesn't necessarily need to be in the system setting format unless that's required for what you are trying to do.  If so then just add an extra format e.g. FormatDate(AddDays(AddMonths("01/"&FormatDate(Today(),"MM/yyyy"), 1),-1), "yyyy-MM-dd") and this will give the output 2021-02-28
 
Hope this helps 🙂

NickNick
Level 3
Hi Michael O'Neil!

Sorry for the late reply. I was away. It is a question with four options. A is the answer but when I tried in the system, it didn't work and resulted in error