Previous months and year if January

Hi,

if I am using this expression:

New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“yyyy”)

→ will it return “2025” when in January 2026?

New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“MM”)

→ will it return “12” when in January 2026?

Thx for explanation.
KR, Vanja

@VanjaV,

To determine what values the expressions New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“yyyy”) and New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“MM”) will return in January 2026, let’s break down what each part of the expression does.

Explanation of the expression steps:

  1. Now.Year and Now.Month: These return the current year and month respectively. In your case, in January 2026, Now.Year will be 2026 and Now.Month will be 1.
  2. New DateTime(Now.Year, Now.Month, 1): This creates a DateTime object representing the first day of the current month. In January 2026, this would be 1st January 2026.
  3. .AddMonths(-1): This subtracts one month from the date. Subtracting one month from 1st January 2026 results in 1st December 2025.
  4. .ToString(“yyyy”): This converts the year part of the date to the string format “yyyy”. Hence, for 1st December 2025, this conversion would give 2025.
  5. .ToString(“MM”): This converts the month part of the date to the string format “MM”. Hence, for 1st December 2025, this conversion would give 12.

So, in summary:

  • New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“yyyy”) will return "2025" when in January 2026.
  • New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“MM”) will return "12" when in January 2026.

LLM Helped here to explain it more clearly. Do let me know still have any query

1 Like

Hi @VanjaV

Your 1st expression will return 2025 in January 2026
1st Expression: New DateTime(Now.Year, Now.Month, 1).AddMonths(-1).ToString("yyyy")

  • Now.Year will return the current year (e.g., 2026 if it’s January 2026).
  • Now.Month will return the current month (e.g., 1 if it’s January).
  • New DateTime(Now.Year, Now.Month, 1) creates a DateTime for the 1st day of the current month (in your case, January 1st, 2026).
  • .AddMonths(-1) subtracts one month from that date, which will take you to December 1st, 2025.
  • .ToString("yyyy") will format the resulting DateTime object as a four-digit year.

Your 2nd expression will return 12 in January 2026
2nd Expression: New DateTime(Now.Year,Now.Month,1).AddMonths(-1).ToString(“MM”)
*Now.Year will return the current year (e.g., 2026 if it’s January 2026).

  • Now.Month will return the current month (e.g., 1 if it’s January 2026).
  • New DateTime(Now.Year, Now.Month, 1)** creates a DateTime object for the 1st day of the current month** (in this case, January 1st, 2026).
  • .AddMonths(-1) subtracts one month from the current date, which will take you to December 1st, 2025 (one month prior to January 1st, 2026).
  • .ToString("MM") formats the resulting DateTime object as a two-digit month. For December, this will return "12".

Hope it helps!!

1 Like

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