How to get previous month's month and year?

Hi!

So I have these drop downs that I need to supply on (Month and Year).

image

My main target is to get previous month’s value and the year it was. I figured out the month but the year is my problem. If I try to extract December 2019, I could get December with this code (Datetime.Now.AddMonths(-1)).ToString(“MMMM”) but the year 2019 is my problem. My current code for Year is now.year.ToString. So the bot will return December 2020 and not December 2019.

Hi,

Can you try the following expression.

(Datetime.Now.AddMonths(-1)).ToString("yyyy")

Regards,

2 Likes

My problem with this is what if today is Feb 2020? I’m afraid your code will return January 2019 and not January 2020

Hi,

My problem with this is what if today is Feb 2020? I’m afraid your code will return January 2019 and not January 2020

No, it will return 2020 in your case. Can you try the following expression?

New date(2020,2,1).AddMonths(-1).ToString("yyyy")

This will return 2020.

Regards,

1 Like

Hi @caduque,

Did you try like this
Now.AddMonths(-1).ToString("MMMM yyyy")

How come bot will return December 2020? Is your system date correct?

1 Like

Hi @aanandsanraj,

Your code worked! Thanks so much!

You are Welcome @caduque :slightly_smiling_face:

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