Month related

Hi all,

I have doubt in changing the month like first my bot has to select previous date so I have used Today.AddDays(-1). If it is 1st August I need to select previous month and date example -month as July and date as 31st how should I do this.

Please help me

Hi,

// Get the current date and time.
DateTime currentDateTime = DateTime.Now;

// Get the previous month.
DateTime previousMonth = currentDateTime.AddDays(-currentDateTime.DayOfWeek + 1);

// Get the 31st of the previous month.
DateTime previousMonth31st = previousMonth.AddDays(-1);

Hi @hanviprebday

Use if condition to check whether today is 1st or not if today is 1st then select the previous month and date.

Hi @hanviprebday

Try this

`previousMonthDate = Today.AddMonths(-1)`

lastDayPreviousMonth = New DateTime(previousMonthDate.Year, previousMonthDate.Month, 1).AddDays(-1)

I hope it helps!!

Thank you all @supriya117 how to check this condition can I used Today.Date =1 in if condition

@hanviprebday

str_date = DateTime.Now.ToString("dd/MM/yyyy").split("/")(0)
then use if conditon:
if str_date.equals("01")
Then get date month and date

@supriya117 what should be the type of the variable str_date I am getting erry

@hanviprebday

It’s a String type.


I am getting this error

@hanviprebday

Its not a “-”(Hyphen)
Its under score(_)

Then also same error

@supriya117 please help me

@hanviprebday

Give like this
Add c in split
DateTime.Now.ToString("dd-MMM-yyyy").split("-"c)(0)

@hanviprebday

Sorry, its my mistake.

Change it to back slash("") because we are getting the date as “dd/MM/yyyy” format.

Hi @hanviprebday ,

can you try this

str = 01-07-2023

DateTime.ParseExact(str,"dd-MM-yyyy",system.Globalization.CultureInfo.InvariantCulture).AddDays(-1).ToShortDateString

output will be 06/30/2023

DateTime.ParseExact(str,“dd-MM-yyyy”,system.Globalization.CultureInfo.InvariantCulture).AddDays(-1).ToString(“dd-MM-yyyy”)

output will be 30-06-2023

Thanks

Thank you all got it