Get the last day from previous month if the current month day one

Hi All,

I need to Get the lastday from previous month if the day one from current month
and if the current day is other then 1st of month no need print last day.
please find the below example

Example: present day 01/03/2024 then get 29/02/2024
Example2: Present day 15/03/2024 then get 15/03/3024

Does anybody have an idea how to do this?
Thank you!

1 Like

Hi,

Try as below:-

Assign activity:
currentDate = DateTime.Now

lastDayOfMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month)

If activity:

Condition: currentDate.Day = lastDayOfMonth
Then:
- Today is the last day of the month
Assign activity
Previous day= currentDate.Adddays(-1)
Else:
Do nothing

Thanks

In general we do DateTime checks consequently at Datetime base. But with the String and 01 Trick we can shortcut the current month first day check:

IF(myDate.ToString("yyyyMMdd").Equals(Now.ToString("yyyyMM01")), myDate.addDays(-1), myDate).Date
1 Like

Hi @sreene26

Assign -> previousMonthLastDay = If (Now.Day = 1, Now.AddDays(-1).AddMonths(-1), Now)
If
  Now.Day = 1
Then
  Write line -> previousMonthLastDay.ToString("dd/MM/yyyy")
Else
  Now.ToString("dd/MM/yyyy")
End If

previousMonthLastDay is of DataType System.DateTime.

Check the below image for better understanding:

Output:
image

Hope it helps!!

1 Like

Hi,

xaml file for your reference:-

Main.xaml (8.8 KB)

Thanks

@sreene26 ,

We have simple function to get this.

Convert.ToDateTime("03/01/2024 00:00:00").AddDays(-1)

image

Thanks,
Ashok :slight_smile:

assign activity

resultDate = If(myDate.Day=1,myDate.AddDays(-1),myDate)

Hope this helps

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.