如何从1月份减去3个月才能获得正确的月份?

Red*_*ket 1 c#

我想在我的网络应用程序中计算年份和月份.

在我的代码中,我有一年201604.我想把这个月减去3,所以它应该是201601.我得到了这部分工作但现在如果我想再次减去另外3个monyths 201601,结果应该是201511.我不知道该怎么做这部分.帮助将不胜感激

码

decimal str = "201604";
decimal year= (decimal) Calculations.ParseStringToDecimal(str.SubString(0,4), 0);
decimal month = (decimal) Calculations.ParseStringToDecimal(str.SubString(4,2), 0);

newMonth = month - 3; 

string newDate = year + month

//Result: 201601. 

//I want to subtract 201601 again, then the result should be 201511. 
Run Code Online (Sandbox Code Playgroud)

Dmi*_*nko 10

使用日期时,使用专门为此目的设计的DateTime类:

  String str = "201604";

  DateTime date = DateTime.ParseExact(str, "yyyyMM", CultureInfo.InvariantCulture)
    .AddMonths(-3);

  // 201601
  String newDate = date.ToString("yyyyMM");
Run Code Online (Sandbox Code Playgroud)