How to get current and last day of every month?

Hi All,

I want to fetch 1st and last day of previous month.
For example: today is 1st December 2021, so i need to fetch 1st November and 30th November.
when it comes on 1st January 2022, I need to fetch 1st December 2021 and 31st December 2021 so on.
I need this in the format (“dd/MM/yyyy”) (01/12/2021).
please some one do needful to do so

Actually, first can be always starting with “1”

For last day you may use

System.DateTime.DaysInMonth(2021,November)
This will give you 30 and that’s the last date of that month

Hope this helps

2 Likes

Hi,

Another solution:
Hope the following helps you.

Last day previous month

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

1st dat previous month

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

Regards,

3 Likes

Assign a integer variable

Numberofdays = System.datetime.daysinmonth(Now.year.tostring , Now.Date.tostring(“MMMM”)

You will get number of days in a month

1 Like

Hi @HeartCatcher

First day of month,

"01/" + now.addmonths(-1).tostring("MM/yyyy")

Last date of previous month,

Datetime.daysinmonth(now.addmonths(-1).tostring("yyyy"),now.addmonths(-1).tostring("MM")) + "/" + now.addmonths(-1).tostring("MM/yyyy")

Thanks

1 Like

Thanks, for replying I need this condition dynamically for entire year like in december I need to fetch 01/11/2021 and 30/11/2021 , in jan 2022 i need to fetch 01/12/2021 and 31/12/2021(dec) when it comes to march 2022 i need ti fetch like 01/02/2022 and 28/02/2022(feb)

Thanks @Yoichi

1 Like

Thanks all for your valuable reply

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