Date in input field to last month (web application)

Hello everyone,

I would like to enter the date of the last month in two fields.

If today is February, I would like the robot to enter the date: 01/01/2024 in the “From” field.

In the “To” field it should then enter 31/01/2024.

My current string always does this for the current month. But I would like to have it for the last month.

Current:
New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString(“dd/MM/yyyy”)

AND second:

New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).ToString(“dd/MM/yyyy”)

Kind regards,
Markus

Hi @Markus_Reimer

Try the below syntax:
To get First date:

New DateTime(now.Year,now.Month,1).AddMonths(-1).ToString("dd/MM/yyyy")

To get last date:

str_lastdate= New DateTime(now.Year,now.Month,1).AddDays(-1).ToString("dd/MM/yyyy")

Hope it helps!!

@Markus_Reimer

Assign activity:
   - fromDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1).ToString("dd/MM/yyyy")
Assign activity:
   - toDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("dd/MM/yyyy")

Hi @Markus_Reimer

Adding to the post

str_firstdate= New DateTime(now.Year,now.Month,1).AddMonths(-1).ToString("dd/MM/yyyy")

str_lastdate= New DateTime(now.Year,now.Month,1).AddDays(-1).ToString("dd/MM/yyyy")

str_firstdate and str_lastdate are of DataType System.String

Regards

Hi @Markus_Reimer

Check the below expression for From field,

- Assign -> FromDate = DateTime.ParseExact(Now.ToString("MMMM yyyy"),"MMMM yyyy",System.Globalization.CultureInfo.InvariantCulture).AddMonths(-1).ToString("dd/MM/yyyy")

Check the below expression for To field,

- Assign -> ToDate = DateTime.ParseExact(Now.ToString("MMMM yyyy"),"MMMM yyyy",System.Globalization.CultureInfo.InvariantCulture).AddDays(-1).ToString("dd/MM/yyyy")

Hope it helps!!

2 Likes

Thank you! It works!

1 Like

It’s my pleasure… @Markus_Reimer

Happy Automation!!

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