How to get the Last date of the Particular Date

Hi Team,

How to find the Last date of the particular Date.
Inputs:
02.02.2023

03.01.2023

Outputs:
28.02.2023

31.01.2023

Regards,
Sai adarsh.

Hi,

Pls try below:

Int daysInMonth = System.DateTime.DaysInMonth(2023,March)

lastDateInMonth = New Date(2023, November, daysInMonth)

where lastDateInMonth variable is of type date.

Mark it as solution if it solves your issue.

Thanks

3 Likes

Hi @adarsh_kotagiri ,

Firstly, Convert the String to DateTime type using the below Expression :

dateValue = Datetime.ParseExact("02.02.2023","dd.MM.yyyy",System.Globalization.CultureInfo.InvariantCulture)

Next, we can use the date variable created to compute the Last date of that month using below :

LastDate = new DateTime(dateValue.Year,dateValue.Month,DateTime.DaysInMonth(dateValue.Year,dateValue.Month))

Here, dateValue and LastDate are both DateTime type variables.

To get the LastDate in the String format required :

LastDate.ToString("dd.MM.yyyy")
1 Like

Hi @adarsh_kotagiri ,

Let’s get first the month and year from your input by splitting the string.

Once we have those, we can now get the last day of the month using this expression in our Assign activity:

lastDayOfTheMonth = System.DateTime.DaysInMonth(CInt(Year),CInt(Month))

Now, we can concatenate the final date: lastDayOfTheMonth.ToString+“.”+Month+“.”+Year

I have also attached the code for your reference.
GetLastDate.xaml (7.3 KB)

Hope this helps. Happy automation!

Kind regards,
Kenneth

1 Like

Hi,

FYI, another approach:
We can easily get last date of the month using ModifyDate activity as the following.

DateTime.ParseExact(strDate,"dd.MM.yyyy",System.Globalization.CultureInfo.InvariantCulture)

Regards,

1 Like

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