Correctly select the previous months & year

Hi all,

I need to extract some data in a website every week. Basically I need the data for 1) current month / year (e.g. May / 2020), 2) last month / year (e.g. April / 2020), 3) last last month / year (e.g. March / 2020).

Picture for your reference:

Currently format:
(1) current month / year: Now.ToString(“MMMM”) + " / " + Now.ToString(“yyyy”)
(2) last month / year: DateTime.Now.AddMonths(-1).ToString(“MMMM”)+" / "+Now.ToString(“yyyy”)
(3) last last month / year: I am still looking for solutions

But as I am looking for the solutions, I found a problem for (2) and (3). If I am to Now.ToString(“yyyy”), I may encounter problems when the current month is, for example, January 2021. In this case, I suppose the output result for (2) would be “December / 2021” instead of “December / 2020”? How can I solve this problem so that the year will change accordingly? And how should the (3) be coded? Thank you!!

Hi,

Hope the following helps you.

Main.xaml (6.0 KB)

Regards,

1 Like

Hi.
How about using DateAdd method not AddMonths?
DateAdd(“m”,-1,Now)

Thank you! That should work well. I just have one question: can thisMonth.ToString(“MMMMM”) be replaced with thisMonth.ToString(“MMMM”)? Any difference between MMMMM & MMMM?

1 Like

Sorry there is one more question… what should I do if I need to also get the last day of the last last month?

With your example xaml file:
For the current month, I can use ThisMonth.ToString(“dd”), and for the last month, I can use lastMonth.ToString(“dd”).

However, for the last last month, when I used lastLastMonth.ToString(“dd”), it doesn’t show the correct last day?

Hi,

Any difference between MMMMM & MMMM?

MMMMM works but we should write MMMM. ( Sorry, it’s my typo)

what should I do if I need to also get the last day of the last last month?

In this case, perhaps we should write as the followng.

thisMonthFirstDay = new DateTime(now.Year,now.Month,1) 
lastMonthLastDay = thisMonthFirstDay.AddDays(-1)
lastLastMonthLastDay = thisMonthFirstDay.AddMonths(-1).AddDays(-1)

Regards,

1 Like

Thank you. Can you confirm my understanding that a total of 6 variables + 6 Assign activities are required in order to perform the tasks (i.e. generating 1) the current & last 2 months / year and 2) the last day of the current & last 2 months)?

1 Like

Hi,

Yes, you are right. If I were to add something, first perhaps we should assign first and last day variables, next month and year string variable using them, as the following.

thisMonthFirstDay = new DateTime(now.Year,now.Month,1) 
lastMonthLastDay = thisMonthFirstDay.AddDays(-1)
lastLastMonthLastDay = thisMonthFirstDay.AddMonths(-1).AddDays(-1)

thisMonthFirstDay.ToString("MMMM")+" / "+thisMonthFirstDay.ToString("yyyy")
lastMonthLastDay.ToString("MMMM")+" / "+lastMonthLastDay.ToString("yyyy")
lastLastMonthLastDay.ToString("MMMM")+" / "+lastLastMonthLastDay.ToString("yyyy")

Regards,

1 Like

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